Reputation: 81
I've been trying to develop a Console Program that Sorts an Array, and then allows the user to search for a specific value within the Array. For the most part, the Sorting Section works fine (although I would like to simplify it to one for loop but it will have to do for now).
However the searching part gives me the number 6487516 every time, no matter what number I put in. I'm certain it has to do with my function find_number, I just do not know what.
#include <stdio.h>
#include <stdlib.h>
#define NOT_FOUND -1
#define num 9
int main(int argc, char *argv[])
{
int ask,how_many;
int user_array[num];
int sorted_array[num];
int i,temp,junction;
printf("type 10 numbers with spaces in between then press enter \n, type the numbers again then press enter, after this press q and then press enter ");
for (i = 0; i<=num ; ++i)
scanf(" %d ",&user_array[i]);
//printf("type ");
for (i = 0; i<=num ; ++i)
scanf(" %d ",&sorted_array[i]);
for (i = 0; i<=num ; ++i)
printf(" A : %d ",user_array[i]);
for (i = 0; i<=num ; ++i)
printf(" B : %d ",sorted_array[i]);
for (i = 0; i <= num; ++i)
{
for (junction = 0; junction <= num - i; junction++)
{
if (sorted_array[junction] > sorted_array[junction+1] )
{
temp = sorted_array[junction];
sorted_array[junction] = sorted_array[junction+1];
sorted_array[junction +1] = temp;
}
}
}
printf (" Left is the Sorted right is the Original");
for (i = 0; i<= num; ++i)
printf(" \n %d, %d ",sorted_array[i],user_array[i]);
printf (" What number do you want to search for?\n");
fflush (stdin);
scanf (" %d",&ask);
printf (" how many numbers? \n");
fflush (stdin);
scanf (" %d",&how_many);
int truth = find_number (sorted_array, ask, how_many);
printf (" %d",&truth);
return 0;
}
int find_number( const int target[10], int goal, int n)
{
int z,found = 0,locate;
int i = 0;
while (!found && i < n)
{
if (target[i] == goal)
found = 1;
else
++i;
}
if (found)
locate = i;
else
locate = NOT_FOUND;
return locate;
}
Upvotes: 2
Views: 778
Reputation: 57774
This is an idiom in C:
#define N 20
int array [N];
for (int j=0; j < N; ++j)
//do something with array[j]
Note that it is not j <= N
. That causes access to an unallocated array element, namely array[N]
.
Upvotes: 2
Reputation: 144695
There are multiple problems in your code:
num
should be defined to 10
for arrays to have 10 elements (!)
you iterate over the array once too many. The classic C idiom is for (i = 0; i < n; i++) { ... }
where n
is the size of the array, ie the number of elements. Since arrays are 0
based, the last valid index in an array is n - 1
.
You have extra spaces in your scanf()
formats, possibly for cosmetic purposes. Trailing have side effects: scanf()
will keep read input until it gets something that is not whitespace. Don't do this.
fflush(stdin);
invokes undefined behavior. Remove it.
The find
function works for any size array, remove the hard coded 10
size in the prototype which is ignored by the compiler anyway.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#define NOT_FOUND (-1)
#define NUM 10
int find_number(const int *target, int goal, int n);
int main(int argc, char *argv[]) {
int ask, how_many;
int user_array[NUM];
int sorted_array[NUM];
int i, temp, junction;
printf("type %d numbers with spaces in between then press enter\n"
"type the numbers again then press enter, "
"after this press q and then press enter", NUM);
for (i = 0; i < NUM; ++i)
scanf("%d", &user_array[i]);
for (i = 0; i < NUM; ++i)
scanf("%d", &sorted_array[i]);
for (i = 0; i < NUM; ++i)
printf(" A : %d\n", user_array[i]);
for (i = 0; i < NUM; ++i)
printf(" B : %d\n", sorted_array[i]);
for (i = 0; i < NUM; ++i) {
for (junction = 0; junction < NUM - i - 1; junction++) {
if (sorted_array[junction] > sorted_array[junction + 1]) {
temp = sorted_array[junction];
sorted_array[junction] = sorted_array[junction + 1];
sorted_array[junction + 1] = temp;
}
}
}
printf (" Left is the Sorted right is the Original\n");
for (i = 0; i < NUM; ++i)
printf("%5d, %5d\n", sorted_array[i], user_array[i]);
printf(" What number do you want to search for?\n");
scanf("%d", &ask);
printf(" how many numbers?\n");
scanf("%d", &how_many);
int truth = find_number(sorted_array, ask, how_many);
printf(" %d\n", &truth);
return 0;
}
int find_number(const int *target, int goal, int n) {
int i;
for (i = 0; i < n; i++) {
if (target[i] == goal)
return i;
}
return NOT_FOUND;
}
Notes:
you should copy the user_array
into the sorted_array
instead of asking the user to enter the numbers again.
you should use a better algorithm for searching in the sorted array.
Upvotes: 1