#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# program that creates 10 integers and then use functions

Member Posts: 4

so i have a program where i have to define Max 10 of an array then find largest,smallest,average,range,and median (getdata function) ask the user for numbers (displaydata) display largest and then display smallest. I am stuck on largest and smallest because no matter what i enter it says they are both 0 can someone help?`

``````                  #include <stdio.h>
#define MAX 10

int getdata(int array[]);
int displaylargest(int array[],int);
int displaysmallest(int array[],int);
void displaydata(int array[],int);

int main ()
{

int array[MAX];
int largest;
int smallest;

printf("\nEnter ten numbers \n\n");

getdata(array);

displaylargest(array,MAX);
displaysmallest( array,MAX);

displaydata(array,MAX );

largest=displaylargest( array,MAX);
printf("\nThe  largest %.2f\n", largest);

smallest=displaysmallest( array,MAX);
printf("\nThe smallest is %.2f\n", smallest);

return 0;
}

int getdata(int array[])

{
int x;
printf ("Enter a number\n ",x+1);
for(x=0;x<MAX;x++)

scanf ("%d",&array[x]);

}

int displaylargest(int array[],int)
{

int x, largest=array[x];

for (x=0; x<MAX; x++)
{
if (array[x]>MAX)

largest=array[x];

}

return(largest);

}

int displaysmallest(int array[],int)

{

int x, smallest=array[x];

for (x=0; x<MAX; x++)
{
if (array[x]<MAX)

smallest=array[x];

}

return(smallest);

}

void displaydata(int array[],int)
{

int x;
for(x=0; x<MAX; x++)
{
printf("%.2f, ",array[x]);

}
}
`
``````
Tagged:

• Member Posts: 35

Hello,

``````#include <stdio.h>
#define MAX 10

int getdata(int array[]);
// this is wrong MAX is global and can be used in functions int displaylargest(int array[], int);
int displaylargest(int array[]);
// same as above int displaysmallest(int array[], int);
int displaysmallest(int array[]);
// same as above
void displaydata(int array[]);

int main() {
int array[MAX];
int largest;
int smallest;
printf("\nEnter ten numbers \n\n");
getdata(array);
// no reason to call this here  displaylargest(array, MAX);
// no reason to call this here  displaysmallest(array, MAX);
displaydata(array);
largest = displaylargest(array);
// it should be %d not %.2f is is not a floating point number
printf("\nThe  largest %d\n", largest);
smallest = displaysmallest(array);
// it should be %d not %.2f is is not a floating point number
printf("\nThe smallest is %d\n", smallest);
return 0;
}
int getdata(int array[]) {
int x;
printf("Enter a number\n ", x + 1);
for (x = 0; x < MAX; x++)
scanf("%d", & array[x]);
}
int displaylargest(int array[]) {
int x = 0;
int largest = 1; // we set this to 1 because if a user enters 1 .. 10 then the if check will see that smallest is = 0 and it will never be updated
// this way it will allow a user to enter 0 and it will be counted as the lowest number. the rest will work as expected. we can eliminate 0 but i would not
// recomend it.
// int x, largest = array[x];
for (x = 0; x < MAX; x++) {
// if (array[x] > MAX)
if (array[x] > largest)
largest = array[x];
}
return (largest);
}
int displaysmallest(int array[]) {
// i would do this instead
int x, smallest = 0;
//int x, smallest = array[x];
for (x = 0; x < MAX; x++) {
//if (array[x] < MAX)
if (array[x] < smallest)
smallest = array[x];
}
return (smallest);
}
void displaydata(int array[]) {
int x;
for (x = 0; x < MAX; x++) {
// it should be %d not %.2f is is not a floating point number
printf("%d, ", array[x]);
}
}
``````
• Member Posts: 35

Please let me know if you have problems or need me to explain something some more.

• Member Posts: 35

Here is fixed code I modified the wrong part in the code above(got largest instead of smallest),

``````#include <stdio.h>
#define MAX 10

int getdata(int array[]);
// this is wrong MAX is global and can be used in functions int displaylargest(int array[], int);
int displaylargest(int array[]);
// same as above int displaysmallest(int array[], int);
int displaysmallest(int array[]);
// same as above
void displaydata(int array[]);

int main() {
int array[MAX];
int largest;
int smallest;
printf("\nEnter ten numbers \n\n");
getdata(array);
// no reason to call this here  displaylargest(array, MAX);
// no reason to call this here  displaysmallest(array, MAX);
displaydata(array);
largest = displaylargest(array);
// it should be %d not %.2f is is not a floating point number
printf("\nThe  largest %d\n", largest);
smallest = displaysmallest(array);
// it should be %d not %.2f is is not a floating point number
printf("\nThe smallest is %d\n", smallest);
return 0;
}
int getdata(int array[]) {
int x;
printf("Enter a number\n ", x + 1);
for (x = 0; x < MAX; x++)
scanf("%d", & array[x]);
}
int displaylargest(int array[]) {
int x, largest = 0; // we set this to 1 because if a user enters 1 .. 10 then the if
// check will see that smallest is = 0 and it will never be updated
// this way it will allow a user to enter 0 and it will be counted
// as the lowest number. the rest will work as expected. we can eliminate 0 but i would not
// recomend it.
// int x, largest = array[x];
for (x = 0; x < MAX; x++) {
// if (array[x] > MAX)
if (array[x] > largest)
largest = array[x];
}
return (largest);
}
int displaysmallest(int array[]) {
// i would do this instead
int x = 0;
int smallest = 1; // we set this to 1 because if a user enters 1 .. 10 then the if
// check will see that smallest is = 0 and it will never be updated
// this way it will allow a user to enter 0 and it will be counted
// as the lowest number. the rest will work as expected. we can eliminate 0 but i would not
// recomend it.

//int x, smallest = array[x];
for (x = 0; x < MAX; x++) {
//if (array[x] < MAX)
if (array[x] < smallest)
smallest = array[x];
}
return (smallest);
}
void displaydata(int array[]) {
int x;
for (x = 0; x < MAX; x++) {
// it should be %d not %.2f is is not a floating point number
printf("%d, ", array[x]);
}
}
``````
• Member Posts: 35

ok, let me clarify, my code will find the lowest and largest numbers that the user entered.

• Member Posts: 35

hum there is a problem i did not notice in smallest numbers selector

• Member Posts: 35

changing int smallest = 1; to int smallest = array[0]; should fix the problem. Setting it to the first value in the array will allow the check to function correctly no matter what.

• Member Posts: 4

thank you so much for your explanation