Jump to content

Help a noob in C !

ive been trying to write this program i found on a past paper. 

I am due to write exams in a couple of months.

 

 

The program should accept no more than 10 numbers and prints each one a separate line.

 

I know this isn't a homework assist forum. BUT... 

 

This is what I have so far

#include <stdio.h>#include <string.h>#include <stdlib.h>int main() {	int arr[10];	int x;	printf ("enter no more than 10 numbers:");	scanf("%d", &x);	if (x < arr[10])		{            for (x=0;x<arr[10];x++)                {                    printf("%d\n",arr[x]);                }}		else {			exit(0);			}	return (0);}

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

So right now, you're taking the input and storing it as a single integer. Then you're comparing that integer to 11th value in the array. If the input value is less than that number then you go on and print stuff. Do you see why this isn't working at all?

 

First, you need to get a list of numbers instead of just one. How are you wanting to take input? Do you just want spaces between the numbers? Or you do want them to hit enter and continue typing single numbers until they're done? Figure that out, and then we can help with the rest of the code as your choice will determine how to do the rest?

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

So right now, you're taking the input and storing it as a single integer. Then you're comparing that integer to 11th value in the array. If the input value is less than that number then you go on and print stuff. Do you see why this isn't working at all?

 

First, you need to get a list of numbers instead of just one. How are you wanting to take input? Do you just want spaces between the numbers? Or you do want them to hit enter and continue typing single numbers until they're done? Figure that out, and then we can help with the rest of the code as your choice will determine how to do the rest?

As i stated, it was a question i found on a past paper. It was pretty vague though about the things you mentioned.

I have revisied the question (It has also been made clear that Im not allow to use arrays to solve the problem) and it mentioned some other stuff about storing the integers in a file.

I will try it again as I do not just want to throw out the question without attempting it properly first.

 

Thank you for your response. Though I had a few of the same questions in approaching the question.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

If it's not specific, then I would guess you get to design it as you want. Taking in input on a single line gets tricky in C since you have to manually parse out the numbers. I would put the scanf in a loop that loops up to 10 times unless they enter a certain character (like n or something).

 

And if you're not allowed to use arrays, I'm not sure how you can do this elegantly unless they are trying to force you to use 'pointers' specifically instead of arrays (even though you can use them interchangeably. You just write it differently). In that case you'll have to allocate 10 ints worth of space and then store them as they are entered at the correct locations within memory. Then loop through the pointer, printing out the values. Hopefully that is not to vague for you to get going.

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

If it's not specific, then I would guess you get to design it as you want. Taking in input on a single line gets tricky in C since you have to manually parse out the numbers. I would put the scanf in a loop that loops up to 10 times unless they enter a certain character (like n or something).

 

And if you're not allowed to use arrays, I'm not sure how you can do this elegantly unless they are trying to force you to use 'pointers' specifically instead of arrays (even though you can use them interchangeably. You just write it differently). In that case you'll have to allocate 10 ints worth of space and then store them as they are entered at the correct locations within memory. Then loop through the pointer, printing out the values. Hopefully that is not to vague for you to get going.

How would you go about looping scanf ? tried nothing worked- Well if worked but it keeps going on after i enter ten set of ints.

Probably because its set to loop looking the number 10. Instead of actually just printing 10 times.

I think pointers might be the solution.

I'm currently doing some reading. It seem a little tricky. We'll see.

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

Use a for loop to in take values and put them in an array.

[ Cruel Angel ]:     Exterior  -   BENQ XL2420T   |   SteelSeries MLG Sensei   |   Corsair K70 RED   |   Corsair 900D  |                                                                                                    CPU:    -   4.7Ghz @ 1.425v             |

                             Interior    -   i7 4770k   |    Maximus VI Formula    |   Corsair Vengeance Pro 16GB    |   ASUS GTX 980 Strix SLIx2  |  840 Pro 512Gb    |    WD Black 2TB  |           RAM:   -   2400Mhz OC @ 1.650v    |

                             Cooling   -   XSPC 120mm x7 Total Radiator Space   |   XSPC RayStorm    |    PrimoChill Tubing/Res  |                                                                                             GPU:   -   1000Mhz @ 1.158            |

Link to comment
Share on other sites

Link to post
Share on other sites

How would you go about looping scanf ? tried nothing worked- Well if worked but it keeps going on after i enter ten set of ints.

Probably because its set to loop looking the number 10. Instead of actually just printing 10 times.

I think pointers might be the solution.

I'm currently doing some reading. It seem a little tricky. We'll see.

 

I mean you literally put it in a loop that loops 10 times.

int* nums;int num;nums = (int*) malloc(sizeof(int) * 10);int i; for(i = 0; i < 10; i++) {     printf("Enter a number: ");     scanf("%d", &num);     *(nums + i) = num; } 

This will prompt exactly 10 times and store the numbers into the next allocated space for nums. You can then just do another loop to print out the numbers. Allowing the user to enter fewer numbers will require you to keep track of how many they do enter (so you don't start printing garbage) as well as give them a way to break out of the scanf loop. I'll leave that to you to spend some more time figuring out.

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

int* nums;int num;nums = (int*) malloc(sizeof(int) * 10);int i; for(i = 0; i < 10; i++) {     printf("Enter a number: ");     scanf("%d", &num);     *(nums + i) = num; } 

i think that a statically allocated array will be a good choice for this particular problem and for a programmer at this particular level

int nums[10];int num;int i; for(i = 0; i < 10; i++) {     printf("Enter a number: ");     scanf("%d", &num);     nums[i] = num; } 

dynamic allocation is cool indeed, but you should avoid it when it's not necessary, especially because it can easily bring up errors, such as forgetting to deallocate the memory with a free() call

Link to comment
Share on other sites

Link to post
Share on other sites

i think that a statically allocated array will be a good choice for this particular problem and for a programmer at this particular level

 

dynamic allocation is cool indeed, but you should avoid it when it's not necessary, especially because it can easily bring up errors, such as forgetting to deallocate the memory with a free() call

 

I obviously would definitely prefer it, but he mentioned that he's not allowed to use arrays for some reason, so that's why used it. 

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

I obviously would definitely prefer it, but he mentioned that he's not allowed to use arrays for some reason, so that's why used it. 

sorry, i didn't read that post from the OP

anyway, pointers used that way are still arrays, so that's not the solution

 

 

I have revisied the question (It has also been made clear that Im not allow to use arrays to solve the problem) and it mentioned some other stuff about storing the integers in a file.

you have to print the valid numbers on a file, and then read them again one by one and print them

 

you have to

  • read a number from the keyboard
  • save it on a file

and do this 10 times, once for every number

 

then, in a seperate loop

  • read a number from the file
  • print it on the screen

10 times too

 

you can use

fprintf

fscanf

Link to comment
Share on other sites

Link to post
Share on other sites

sorry, i didn't read that post from the OP

anyway, pointers used that way are still arrays, so that's not the solution

 

 

you have to print the valid numbers on a file, and then read them again one by one and print them

 

you have to

  • read a number from the keyboard
  • save it on a file

and do this 10 times, once for every number

 

then, in a seperate loop

  • read a number from the file
  • print it on the screen

10 times too

 

you can use

fprintf

fscanf

 

 

I mean you literally put it in a loop that loops 10 times.

int* nums;int num;nums = (int*) malloc(sizeof(int) * 10);int i; for(i = 0; i < 10; i++) {     printf("Enter a number: ");     scanf("%d", &num);     *(nums + i) = num; } 

This will prompt exactly 10 times and store the numbers into the next allocated space for nums. You can then just do another loop to print out the numbers. Allowing the user to enter fewer numbers will require you to keep track of how many they do enter (so you don't start printing garbage) as well as give them a way to break out of the scanf loop. I'll leave that to you to spend some more time figuring out.

Thanks alot guys. I definitely have to read up on pointers now to fully understand how i can further implement it. as an array alternative for a question like this. Thanks again for all your suggestions and help. 

I dream of 0s and 1s folding to my every command,

algorithms seeping from the back of my head when I need them.

Link to comment
Share on other sites

Link to post
Share on other sites

I think doing it like @Naty722 suggested (nums = (int*) malloc(sizeof(int) * 10);) could still be considered using arrays.

Usually the other way to do it is with lists. A struct for each number with a pointer to the next struct.

If the question says to use files then @Ciccioo 's answer looks fine. However, saving inputs in a file to treat them later is not very usual, considering the overheads.

Link to comment
Share on other sites

Link to post
Share on other sites

I think doing it like @Naty722 suggested (nums = (int*) malloc(sizeof(int) * 10) ;) could still be considered using arrays.

Usually the other way to do it is with lists. A struct for each number with a pointer to the next struct.

If the question says to use files then @Ciccioo 's answer looks fine. However, saving inputs in a file to treat them later is not very usual, considering the overheads.

i guess it's just an I/O exercise to learn how to use console and files

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×