+ 1

Write a C program to enter 30  marks from user using one dimensional array, and write all numbers  starting from 30th to first

Can I have help 🤍

21st Dec 2021, 6:05 PM
LAMA ALYAZJI
7 Réponses
+ 2
Which parts of the program are you having trouble with? Have you written a flow chart (or psuedocode) so you know what you are trying to do? Do you have experience in getting user input? Storing that input to an array? Accessing (traversing) the array from last to first? (I would probably use --i for my increment and start my for loop at i=30)
21st Dec 2021, 10:03 PM
HungryTradie
HungryTradie - avatar
+ 1
When you take input your go-to will probably be scanf("%d", &array), be careful. A user can enter a non-digit char and crash the program.
22nd Dec 2021, 9:39 PM
William Owens
William Owens - avatar
+ 1
@Jeff.C Does the explanation make sense?
23rd Dec 2021, 12:43 PM
William Owens
William Owens - avatar
0
G'day William, I'm still learning, but could OP use: if (array[i]%1==0) to check the number is an integer? Would that still error? Can we use "try" in C, or "while"? Or maybe an "if" statement could "return" a non-zero value so the "main()" function reacts to the unexpected input? Seems I've still got a lot to learn.... 👍
22nd Dec 2021, 11:03 PM
HungryTradie
HungryTradie - avatar
0
1 of 2: Lets say he uses the following: Lots can go wrong with this code.... #include <stdio.h> int main (void) { int num_list[5]; for(int i = 0; i < 5; i++) { printf("Please enter number %i of the list: ", (i + 1)); scanf("%d", &num_list[i]); } puts("\nYou entered the following numbers:"); for(int i = 0; i < 5; i++) { printf("%i: %i\n", (i + 1), num_list[i]); } } In this example if the user was friendly and only entered whole numbers like Please enter number 1 of the list: 12 Please enter number 2 of the list: 23 Please enter number 3 of the list: 32 Please enter number 4 of the list: 42 Please enter number 5 of the list: 23 The output would be: You entered the following numbers: 1: 12 2: 23 3: 32 4: 42 5: 23 However since we have conversion specification ("%d") the code is expecting only whole numbers thus if it receives a non-digit char it will stop accepting input and in this case would print garbage (left over stuff in the memory locaitons) like the following: Please enter number 1 of the list: d Please enter number 2 of the list: Please enter number 3 of the list: Please enter number 4 of the list: Please enter number 5 of the list: The output wont be the same on your computer but mine for this instance is: 1: 0 2: 0 3: -1522417472 4: 22020 5: 457093664 The numbers are just garbage because scanf stopped receiving input after the 'd' thus none of the memory location in the array have valid data for the output. using if(array[i]%1==0) would only work if the data we typed went in the memory location. Thus we have to determine how to accept the appropriate input. adding a variable (the return value of scanf) and changing our loop we can protect the input from crashing the program. **crashing may be the incorrect technical term for what its doing **
23rd Dec 2021, 2:37 AM
William Owens
William Owens - avatar
0
2 of 2: Example change: int is_num; for(int i = 0; i < 5; i++) { printf("Please enter number %i of the list: ", (i + 1)); do { is_num = scanf(" %d", &num_list[i]); while(getchar() != '\n'); } while (is_num != 1); } using this loop we could do the following: Please enter number 1 of the list: d 23 Please enter number 2 of the list: 2.5 Please enter number 3 of the list: 45e6 Please enter number 4 of the list: 19 Please enter number 5 of the list: 20 This resulted in this output: You entered the following numbers: 1: 23 2: 2 3: 45 4: 19 5: 20 In the first index 'd' did not crash it instead waited for proper input In the second index scanf stopped at the first non-digit char, the '.' The third did the same at 'e' Hope this helps. With this loop though you have to enter the 5, I believe the original question was up to 30 which means the user may enter 0 to 30 marks.
23rd Dec 2021, 2:42 AM
William Owens
William Owens - avatar
0
William Owens yep, great examples. Looks like it isn't like the Python error catching (that I have forgotten...), But simply ignores the input that isn't what it is expecting. Nice one.
23rd Dec 2021, 7:24 PM
HungryTradie
HungryTradie - avatar