Reputation: 101
I'm trying to write a program that will read in the number of values for an array, call a function to input said values and then compute the average. From there it will call another function to print off a corresponding message. The problem is that it appears to be returning what would be a memory address.
Thanks in advance!
Here is my code:
#include <stdio.h>
#include <math.h>
int compute_grade_avg(int grades[], int size)
{
int i, result, average, sum=0;
//Ask the user for the specific grades
for(i=0;i<size;i++){
printf("Enter a grade:\n");
scanf("%d", &grades[i]);
}
//Calculate the sum of values in the array
for(i=0;i<size;i++){
sum += grades[i];
}
//Calculate the average based off of the sum and size of the array
average = sum/size;
result = ceil(average);
return result;
}
//Function to print the corresponding message
int write_grade_message()
{
int average;
//Messages to print depending on grade
if (average < 60){
printf("Failed semester - registration suspended");
}
else if(average < 69){
printf("On probation for next semester");
}
else if(average < 79){
printf("");
}
else if(average < 89){
printf("Dean's list for the semester");
}
else if(average < 100){
printf("Highest honors for the semester");
}
return(0);
}
//Main Function
int main()
{
int size, average;
//Enter the number of grades that will go into the array \
printf("Enter the number of grades to be entered:\n");
scanf("%d", &size);
int grades[size];
//Call compute grade average function
average = compute_grade_avg(grades, size);
//Call write grade message function
write_grade_message(average);
return(0);
}
Upvotes: 0
Views: 1021
Reputation: 1082
warning: 'average' is used uninitialized in this function [-Wuninitialized],
this is because at no time of the function the user is asked the average
value.
Looking at the code a little, we see that the variable average
is calculated as average = compute_grade_avg (grades, size);
. Consequently, what must be done is to pass to function write_grade_message the variable average
. It would be like this int write_grade_message (int average).
Second, "" would be an empty string, but an empty string is not defined like this in C, but it occurs to me to put a space "" between the two quotes. Third, the comment Enter the number of grades that will go into the array
should be commented / * * / and not //
.
The whole program would be like that:
#include <stdio.h>
#include <math.h>
int compute_grade_avg(int grades[], int size)
{
int i, result, average, sum=0;
//Ask the user for the specific grades
for(i=0;i<size;i++){
printf("Enter a grade:\n");
scanf("%d", &grades[i]);
}
//Calculate the sum of values in the array
for(i=0;i<size;i++){
sum += grades[i];
}
//Calculate the average based off of the sum and size of the array
average = sum/size;
result = ceil(average);
return result;
}
//Function to print the corresponding message
int write_grade_message(int average)
{
//int average=0;
//Messages to print depending on grade
if (average < 60){
printf("Failed semester - registration suspended");
}
else if(average < 69){
printf("On probation for next semester");
}
else if(average < 79){
printf(" ");
}
else if(average < 89){
printf("Dean's list for the semester");
}
else if(average < 100){
printf("Highest honors for the semester");
}
return(0);
}
//Main Function
int main()
{
int size, average;
/*Enter the number of grades that will go into the array */ \
printf("Enter the number of grades to be entered:\n");
scanf("%d", &size);
int grades[size];
//Call compute grade average function
average = compute_grade_avg(grades, size);
//Call write grade message function
write_grade_message(average);
return(0);
}
Upvotes: -1
Reputation: 91
The call of write_grade_message(average);
is passing a parameter average.
However, the function has no parameter, instead a declaration of a local variable average which is not initialized.
the function should be update like this.
int write_grade_message(int average)
{
// int average; ======> then removed this line
Upvotes: 1
Reputation: 53016
The problem is specifically that you passed a paramter to int write_grade_message()
that takes an unspecified number of parameters, which makes the code compile but has as it is now undefined behavior.
There is yet another source for undefined behavior to happen, because you redeclared average
inside write_grade_message()
and you tried to read the value without previously initializing it1. You need to change write_grade_message()
to read
void write_grade_message(int average) ...
and remove the internal declaration of average
and your code will probably work just fine, also read the comments to your question for more useful tips.
If your code compiled cleanly and without issuing any warnings or messages, then you need to enable your compiler's warnings and diagnostics, which not only help you see through some silly mistakes that you can make sometimes, but also should help you learn the language deeply, since you can infer from the warning messages some useful knowledge about the language.
1In C, not initializing a variable explicitly — except for global variables, or variables with the static
storage class — would leave the variable uninitialized. Any attempt to use it other than initialize it, would invoke what is called undefined behavior
Upvotes: 1
Reputation: 13590
Your problem is that int write_grade_message();
is a function that can take
any number of parameters but you won't see them inside write_grade_message
.
You have to declare it like this:
int write_grade_message(int average)
{
// your code
if (average < 60){
printf("Failed semester - registration suspended");
}
else if(average < 69){
printf("On probation for next semester");
}
else if(average < 79){
printf("");
}
else if(average < 89){
printf("Dean's list for the semester");
}
else if(average < 100){
printf("Highest honors for the semester");
}
return(0);
}
Also note that in compute_grade_avg
the variables average
, sum
and size
are of type int
. Division with int
s returns you always an int
, so ceil
won't round up the number. You would need to use floating points for that.
However you have to change the type of the
function to return float
and result
and average
must be float
s too.
float compute_grade_avg(int grades[], int size)
{
float result, average;
...
average = ((float) sum)/size;
result = ceil(average);
return result;
}
Upvotes: 0