Returning values in array in c

  • Thread starter konvictx
  • Start date
  • Tags
    Array
In summary, your code is returning an average, maximum and minimum temperature for a set of values, but it's not returning the values for minimum and maximum.
  • #1
konvictx
1
0
Code:
#include<stdio.h>
#include<conio.h>
float average(float[],float );
float highest(float[],float );
float lowest(float[], float );
void main (void)
{
float temper[15];
float x,n, y, z;
int  day;
clrscr();
printf("enter the no.days to read temperature for=\n");
scanf("%d",&n);
for(day=0; day<n; day++)
 {
 printf("temperature for day%d=",day+1);
 scanf("%f",&temper[day]);
 }
x=average(temper,n);
printf("average temperature is=%.2f\n",x);
y=highest(temper,n);
printf("highest temperature is=%.2f\n",y);
z=lowest(temper,n);
printf("lowest temperature is=%.2f\n",z);
getche();
}

float average(float temper[],float n)
{
int sum=0.0,x;
float average;
for(x=0; x<n; n++)
 sum+=temper[x];
 average=sum/n;
 return (average);
}

float highest(float temper[],float n)
{
float highest;
int y;
highest=temper[0];
for(y=0; y<n; y++)
if(highest<temper[y])
highest=temper[y];
return(highest);
}

float lowest(float temper[],float n)
{
float y,lowest=temper[0];
for(y=0; y<n; y++)
if(lowest>temper[y])
lowest=temper[y];
return(lowest);
}

hy my code is not returning value of average minimum and maximum temperature. what can i do now?
 
Technology news on Phys.org
  • #2
What's it returning?
 
  • #3
What result does your code actually show for maximum, minimum and average, for a specified small set of temperature values? (give a specific numeric example)
 
  • #4
Code:
void main (void)
{
   float temper[15];
   float x,n, y, z;
   int  day;
   clrscr();
   printf("enter the no.days to read temperature for=\n");
   scanf("%d",&n);
.
.
.
You are declaring n as a float, but are attempting to read an int in scanf. Change the declaration of n to int.

I didn't notice any other errors, but I only gave your code a quick scan.
 
  • #5
There is a Data Typical Mistake in float average();

You see, when you declare int sum = 0.0; sum is of int data type.

Now, when you store float into int, as in sum+=temper[x];
sum will truncate/delete all the decimal values and only sum integer part.

sum/n is ok, it will give a decimal float value.

If you declare n as int, and fix the datatypes in all of the functions, you still don't have to bother about sum/n, because sum is now of float type.

So, do all those edits, and tell us how's it going.

Editted:

Code:
#include<stdio.h>
#include<conio.h>
float average(float[],int );
float highest(float[],int);
float lowest(float[], int);
void main (void)
{
float temper[15];
float x, y, z;
int  day, n;
clrscr();
printf("enter the no.days to read temperature for=\n");
scanf("%d",&n);
for(day=0; day<n; day++)
 {
 printf("temperature for day%d=",day+1);
 scanf("%f",&temper[day]);
 }
x=average(temper,n);
printf("average temperature is=%.2f\n",x);
y=highest(temper,n);
printf("highest temperature is=%.2f\n",y);
z=lowest(temper,n);
printf("lowest temperature is=%.2f\n",z);
getche();
}

float average(float temper[],int n)
{
 int x;
 float sum=0.0;
float average;
for(x=0; x<n; n++)
 sum+=temper[x];
 average=sum/n;
 return (average);
}

float highest(float temper[],int n)
{
float highest;
int y;
highest=temper[0];
for(y=0; y<n; y++)
if(highest<temper[y])
highest=temper[y];
return(highest);
}

float lowest(float temper[],int n)
{
float y,lowest=temper[0];
for(y=0; y<n; y++)
if(lowest>temper[y])
lowest=temper[y];
return(lowest);
}

Now that should do.
 

What is an array in C?

An array in C is a data structure that allows you to store multiple values of the same data type in a sequential manner. It is a fixed-size collection of elements, with each element having a unique index starting from 0.

How do you declare an array in C?

To declare an array in C, you use the syntax data_type array_name[array_size]; where data_type is the type of data you want to store and array_size is the number of elements you want the array to hold.

How do you assign values to an array in C?

To assign values to an array in C, you can use a for loop or scanf() function. For example, you can use for loop to initialize the array with values or use scanf() to get input from the user and assign it to the array elements.

How do you access elements in an array in C?

You can access elements in an array in C using the index of the element. The first element in an array has an index of 0, the second element has an index of 1, and so on. You can also use a for loop to iterate through the array and access each element.

How do you return values in an array in C?

To return values in an array in C, you can use a return statement in a function that has an array as the return type. Alternatively, you can pass the array as a parameter to a function and modify its values within the function, then access the modified array after the function returns.

Similar threads

  • Programming and Computer Science
Replies
4
Views
729
  • Programming and Computer Science
Replies
4
Views
895
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
4
Replies
119
Views
15K
Back
Top