C programming using arrays and functions

]Write a C program that accepts 8 user input ratings on a new game. Print the lowest and highest ratings received. Use functions.

I new to this and I don't fully understand this topic. it's more to the question but I just to first part because I wanted to ensure I'm on the right path. when i put in values to test the data it's not replacing highest or lowest it's just saying "The highest is 0.0" "The lowest is 0.0"

[#include <stdio.h>
double LowestAndHighest (double num);


void main(){
double num;
double RatingReceived =LowestAndHighest(num);
printf ("%lf", RatingReceived);

}

double LowestAndHighest(double num){
double Lowest=0.0;
double Highest=0.0;
double i, rating[8];
while (num!= -1)/*end program if this value is entered*/ {

for (i=0; i<8; i++){
printf ("Enter ratings for S5\n");
scanf ("%lf", &rating[i]);

if (rating[i] >= Highest) {

Highest=rating[i];

if (rating[i] <= Lowest)

Lowest=rating[i] ;

} //end if
printf ("The Highest is %lf \n", rating[i]);
printf ("The Lowest is %lf \n", rating[i]);


} //end for


} // end while
return Highest;
return Lowest;
}]
Last edited on
Please use [code] tags.

There is no "%lf" output format specification. It should read

printf("The Highest is %f\n", Highest);

etc.


You also need to try to compile your program and pay attention to the errors you get. For example, what does the compiler say about i?

Once you return, nothing after that gets done. (The second "return" statement is ignored.) You can't return more than one thing from a function that way anyway. A function returns one value. You are trying to return two.

What does the argument "num" do?
(Currently, nothing. It is ignored -- which is good, because its initial value is not specified, so it could be anything.)


Once you fix those things, here is some structural advice.

Your function is trying to do too many things. Have a function that does nothing more than get a list of numbers from the user. Have a function that does nothing more than find the largest value in a list of numbers. Etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
int get_array_from_user( double a[], int max );

double get_lowest( double a[], int n );

double get_highest( double a[], int n );

int main()
{
  double a[ 8 ];
  int size = get_array_from_user( a, 8 );
  printf( "The highest is %f\n, get_highest( a, size ) );
  printf( "The lowest is %f\n, get_lowest( a, size ) );
}

Hope this helps.
Topic archived. No new replies allowed.