Reputation: 57
but currently I am experiencing the wrong output, I have double checked that user input was in the array but the result displayed as "no number matching".
How can I solve this problem?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void arrayresult(int arraylength[], int &input_num)
{
for (int i = 0; i < 10; i++)
{
if (arraylength[i] == input_num)
{
printf("The number you entered is in the array");
break;
}
else
{
printf("The number you entered is not in the array");
break;
}
}
}
int main(void)
{
int input_num;
int arrayindex;
int arraylength[10];
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
arrayindex += 1;
arraylength[i] = rand() % (99 + 1 - 11) + 11;
printf("A[%d]: %d ", arrayindex, arraylength[i]);
}
printf("\n");
printf("Enter the number you want to search: ");
scanf("%d", &input_num);
arrayresult(arraylength, input_num);
return 0;
}
Upvotes: 1
Views: 89
Reputation: 483
Simply,Your program tend to check only the first index of the arraylength with that input number. If its fails you are out of the for loop. Time to know about the uses of flag variable. If Flag is set, the number is already found, and don't wants to check again. If Flag is unset, the number is not found.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void arrayresult(int arraylength[], int input_num)
{
int flag = 0;
for (int i = 0; i < 10; i++)
{
if (arraylength[i] == input_num)
{
printf("The number you entered is in the array");
flag = 1;
break;
}
}
if (flag == 0)
{
printf("The number you entered is not in the array");
}
}
int main(void)
{
int input_num;
int arrayindex = 0;
int arraylength[10];
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
arrayindex += 1;
arraylength[i] = rand() % (99 + 1 - 11) + 11;
printf("A[%d]: %d ", arrayindex, arraylength[i]);
}
printf("\n");
printf("Enter the number you want to search: ");
scanf("%d", &input_num);
arrayresult(arraylength, input_num);
return 0;
}
Hope this might helps : )
Upvotes: 0
Reputation: 183
This code might help you.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void arrayresult(int arraylength[], int input_num)
{
for (int i = 0; i < 10; i++)
{
if (arraylength[i] == input_num)
{
printf("The number you entered is in the array");
return;
}
}
printf("The number you entered is not in the array");
}
int main(void)
{
int input_num;
int arraylength[10];
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
arraylength[i] = rand() % (99 + 1 - 11) + 11;
printf("A[%d]: %d ", i + 1, arraylength[i]);
}
printf("\n");
printf("Enter the number you want to search: ");
scanf("%d", &input_num);
arrayresult(arraylength, input_num);
return 0;
}
Upvotes: 0
Reputation: 502
void arrayresult(int arraylength[], int &input_num)
{
for (int i = 0; i < 10; i++)
{
if (arraylength[i] == input_num)
{
printf("The number you entered is in the array");
break;
}
else
{
printf("The number you entered is not in the array");
break;
}
}
}
in this for loop,if your input not match the arraylength[0],
then your method immediately print 'the number you entered is not in the array'
and then break the for loop,so you never get the chance to compare the rest of your array to input_num
Upvotes: 0
Reputation: 8232
your logic to check-in array is wrong. try this
void arrayresult(int arraylength[], int input_num) {
bool isFound = false;
// iterate over till you found the number or reaches end of array
for (int i = 0; i < 10; i++){
if (arraylength[i] == input_num){
isFound = true;
break;
}
}
if(isFound == true) {
printf("The number you entered is in the array");
}else{
printf("The number you entered is not in the array");
}
}
Upvotes: 1
Reputation: 518
You can only conclude the number is not the list, after checking with all elements remove else from for loop.
else
{
printf("The number you entered is not in the array");
break;
}
write printf("not found") after the loop, and write return in if
Upvotes: 0