Hi everyone, I am currently learning C code and come a cropper.
I want to write a code where 5 integers are stored in an array and the user has to input any of them to show a message on the screen saying “correct value”
Could anyone help?
Cheers
Hi everyone, I am currently learning C code and come a cropper.
I want to write a code where 5 integers are stored in an array and the user has to input any of them to show a message on the screen saying “correct value”
Could anyone help?
Cheers
Can you declare an array?
Can you print a prompt?
Can you input an integer?
In short, show us how far you can get by yourself.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:#include <stdio.h> #include <stdlib.h> int main() { int IntS[] = {1,2,3,4}; int IntI[4], i; while (1) { printf("Enter Integer: \n"); for (i=0;i<3;i++) { scanf("%d", &IntI[i]); if (IntS[i]!= IntI[i]) { printf("Wrong Integer\n\n"); } else { printf("Correct Integer \n"); } } } }
Last edited by Jordan99; 10-16-2020 at 03:36 AM.
You didn't show the array declaration.
But perhaps more importantly, have you compiled your code and tried to run it? You need to take things slowly: write some code, make sure it compiles, and if feasible, run it and enter test input and observe the test output to see if you're on the right track. You shouldn't be writing a bunch of code all at once when you don't yet have the grasp of how it works.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Apologies, i have changed it.
Yes the code runs correctly but it only compares the entered integer with the integer in the array in order.
i wanted to know how to change the code so it recognises any integer at any time.
Thanks
Okay, great. One of the things you should learn sooner or later is how to format your code to make it readable. One important aspect of this is to use indentation to denote scopes, like what is the body of a function, or a loop, or an if statement. For example:
As you can see, the code within the body of the main function is indented by one level. Then when we enter the while loop, it is indented by yet another level, and then goes down by one level when we're out of the loop. This way, it is easier to trace what is nested in what, which makes it easier to reason about the code because you have less to worry about whether say, the for loop is inside the while loop or does it come after the while loop. (Of course, typo errors concerning indentation itself can still lead to related mistakes in interpreting the code, but they also tend to be easier to spot with otherwise good indentation.)Code:#include <stdio.h> #include <stdlib.h> int main() { int IntS[] = {1, 2, 3, 4}; int IntI[4], i; while (1) { printf("Enter Integer: \n"); for (i = 0; i < 3; i++) { scanf("%d", &IntI[i]); if (IntS[i] != IntI[i]) { printf("Wrong Integer\n\n"); } else { printf("Correct Integer \n"); } } } }
Next, you have two arrays, but according to what you described, you only need one array, and it should have 5 elements, not 4 elements. Furthermore, when you loop over the array, you should loop over all the elements of the array, not only 3 of them. One way to avoid such mismatch is to compute the number of elements in the array and then use that constant, e.g.,
Consequently, when you want to read an integer from the user, you only need one integer, not an array of them, and you only need to read this integer once, not in a loop (unless you're looping to give the user several tries, but that is something you should only do after you have successfully gotten the code to work with a single try).Code:int numbers[] = {1, 2, 3, 4, 5}; const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]); // ... for (size_t i = 0; i < num_numbers; i++) { // use numbers[i] }
The for loop itself should be search the array of numbers to check if the number the user entered is equal to any one of them. If it is, that's when you announce that the value entered is correct and break from the loop. If none are, that's when you tell the user that value entered is not correct, i.e., you only need to do this once, not on each iteration.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi.
Thanks for the help!
This is the code i have come up with but it still acts the same as before.
I think it could be to do with this section but not 100%Code:#include <stdio.h> #include <stdlib.h> int main() { int numbers[] = {1,2,3,4,5}; const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]); int numberin, i; while (1) { printf ("Enter Integer:"); for (size_t i = 0; i < num_numbers; i++) { scanf("%d", &numberin); if (numbers[i] != numberin) { printf("Wrong Integer\n\n"); } else { printf("Correct Integer \n"); } } } }
Code:if (numbers[i] != numberin) { printf("Wrong Integer\n\n"); } else { printf("Correct Integer \n");
Last edited by Jordan99; 10-19-2020 at 01:58 PM.
You can refer to my previous post:
The for loop itself should be search the array of numbers to check if the number the user entered is equal to any one of them. If it is, that's when you announce that the value entered is correct and break from the loop. If none are, that's when you tell the user that value entered is not correct, i.e., you only need to do this once, not on each iteration.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hey, sorry to be a pain this is my first coding practice.
This is the code I have come up with.
it still doesn't work.
any ideas?Code:#include <stdio.h> #include <stdlib.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int entnumbers; const size_t num_numbers = sizeof(numbers) / sizeof(numbers[0]); printf("Please enter number"); scanf("%d", entnumbers); for (size_t i = 0; i < num_numbers; i++) { for (entnumbers = numbers; entnumbers != numbers;) printf("Correct Integer"); } }
1. How does it not work? You need to be absolutely clear what your code is supposed to do and why you concluded it does not work.
2. Trace through your code step by step. This is with reference to #1. That is, at each step you record the current value of the variables and the output thus far so you can truly understand what your code is doing.
3. Do you understand everything that I wrote in previous posts? I outlined the algorithm in words and I didn't frivolously mention keywords like "check if the number the user entered is equal to any one of them" and "break from the loop", but you didn't use these in your most recent code.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hint: "break" is an C keyword.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson