r/CodingHelp • • Jun 24 '19

[C] Please help me writing code for this?

Post image
15 Upvotes

46 comments sorted by

15

u/[deleted] Jun 24 '19 edited Nov 06 '24

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

1

u/thatlowkey Jun 24 '19

Thanks!! What if the user is free to input any number and the output till 9 will be for eg: "nine" and for >9 it outputs "greater than 9"?

14

u/MrQuickLine CSSophile Jun 24 '19

The guy you're replying to is teasing you. He knows what you're asking for. There's a rule around here about no unethical questions. We don't do people's homework. You need to try to make it work yourself, and we'll help if you get stuck. We're not going to do all the work for you if you are not even attempting to understand what's being asked of you.

3

u/thatlowkey Jun 24 '19
#include <stdio.h>

static const char *strings[] = {"one","two","three","four","five",
                            "six","seven","eight","nine"};
int main()
{ 
int n = 0;
if (scanf("%d",&n) < 1)
    return 1;

if (n >= 1 && n <= 9)
    printf("%s",strings[n-1]);
else
    printf("Greater than 9");

return 0;
}

2

u/thatlowkey Jun 24 '19

but this doesn't work

1

u/KuntaStillSingle Jun 25 '19

TBH I don't use C++, would replacing line 3 with:

static const strings[9] = {"one","two","three","four","five",
                        "six","seven","eight","nine"};

Work?

1

u/thatlowkey Jun 25 '19

It's C. I'll let you know

1

u/KuntaStillSingle Jun 25 '19

Ah nvm I think you need static const string strings[9] ....

1

u/thatlowkey Jun 25 '19

I've tried that didn't work

1

u/KuntaStillSingle Jun 25 '19

Is it refusing to compile?

→ More replies (0)

1

u/ker001 Jun 24 '19
How do you type like that? or is it a pic?

never mind, i found out.

1

u/thatlowkey Jun 24 '19

It's not a pic. I typed it out on my laptop

1

u/thatlowkey Jun 24 '19

But this code doesn't work

2

u/voords Jun 24 '19

Why not ?

1

u/thatlowkey Jun 25 '19

That's y I've asked

1

u/rongrongpa Jun 25 '19

why there's a return 1 after scanf?
why n-1 when accessing the array?

3

u/KuntaStillSingle Jun 25 '19

why there's a return 1 after scanf

That's an exit code, if you give an input less than 1 to op's program it will end the main function and return 1, which indicates to the host environment the program exited with some error.

why n-1

If he inputs 1, he wants the string corresponding to 1, "one", which is at index 0. The string corresponding to 2, "two", is at index 1, so he wants the n-1 index to return the correct response.

I am thinking the issue may be with:

static const char *strings[] = {"one","two","three","four","five",
                        "six","seven","eight","nine"};

At least what I can tell from this link, when creating an array of strings as a char array, you want to specify the number of elements and the max number of characters per element? https://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/

Or are those values considered implicit because the array is constant?

1

u/thatlowkey Jun 26 '19

Thanks! Actually I tried implementing 'for' loop but it starts giving numeric value. Will I have to put 'if' statement too?

2

u/KuntaStillSingle Jun 26 '19

Is there any possibility you could post your code and output?

1

u/thatlowkey Jun 26 '19
int main(){
static const char *strings[9]= 
{"one","two","three","four","five","six","seven","eight","nine"};
int i=0;
scanf("%d",&i);
if(i<=9)
printf("%d",strings[i]);
if(i>9)
printf("greater than 9");
}

1

u/thatlowkey Jun 26 '19

Actually I was entering %d for string that's why was getting numeric value 😅 Now when I enter %s, output is 'one'

→ More replies (0)

1

u/rongrongpa Jun 26 '19

ahh yes, you're right
i didnt really look at the array of strings, i thought there's a "zero" inside, so it would cater the case of input 0

1

u/thatlowkey Jun 25 '19

Is it wrong

2

u/rongrongpa Jun 25 '19

those question is my clue for your code.

if you still stuck, you can start from that

1

u/[deleted] Jun 24 '19

:)

4

u/ker001 Jun 24 '19 edited Jun 24 '19
include <iostream>

using std::cout;

int main() {
int n;
do{
cout<<"Enter your nubmer";
cin>>n;

switch(n){
 case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6;
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight"; 
break;
 }
}
while(n<9);

cout << "Greater than 9"';

}

edit: tried to make it look presentable

1

u/thatlowkey Jun 27 '19

It doesn't print anything :(

1

u/ker001 Jun 27 '19

Then learn coding basics first. I intentionally left some thing's that are obvious, but maybe something else is missing.

There are a lot of free courses online so look it up.

This is only one idea behind your question, there can be a lot of other approaches to the problem and that's what I like in programming.

1

u/thatlowkey Jun 27 '19
int main(){
int n;
scanf("%d",&n);
switch(n){
    case 1:
    printf("one");
    break;
    case 2:
    printf("two");
    break;
    case 3:
    printf("three");
    break;
    case 4:
    printf("four");
    break;
    case 5:
    printf("five");
    break;
    case 6:
    printf("six");
    break;
    case 7:
    printf("seven");
    break;
    case 8:
    printf("eight");
    break;

}

while(n>9)
printf("greater than 9");
}

1

u/ker001 Jun 27 '19

Well done 👌

1

u/thatlowkey Jun 27 '19

please tell where it goes wrong

2

u/ker001 Jun 27 '19 edited Jun 27 '19

You're missing return 0; And compailer will tell you if there's any more mistakes Edit, also that's supposed to be do while cycle You type do {this} while(therm) do{ Everything you wamt to be looped } while(you hit that condition for exoting the loop aka n<9 in this case) ; return 0; }

1

u/thatlowkey Jun 28 '19

Thanks a lot for your help

2

u/zenith4395 Jun 25 '19

Better to redo the entire thing rather than listen to advice, I guess... Again, not sure what was causing your error, but your refusal to try and find out led us here. Everybody here is right, a switch case for each number between one and nine with a print of their respective word is the right way, with the default case being “greater than nine”