Returning values in array in c

  • Thread starter Thread starter konvictx
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around a C programming code intended to calculate the average, highest, and lowest temperatures from user input. Participants are examining issues related to data types, variable declarations, and the functionality of the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions why the code is not returning the values for average, minimum, and maximum temperatures.
  • Another participant asks for the actual output of the code with a specific set of temperature values to better understand the issue.
  • A suggestion is made to change the data type of the variable 'n' from float to int, as it is being read as an integer from user input.
  • A participant points out a mistake in the declaration of 'sum' as an integer, which could lead to truncation of decimal values when summing the temperatures.
  • Further edits to the code are proposed, including changing the data types in function declarations and ensuring that 'sum' is declared as a float to avoid data loss.

Areas of Agreement / Disagreement

Participants express differing views on the specific issues in the code, with some agreeing on the need to change data types while others focus on the functionality of the code. The discussion remains unresolved regarding the complete correctness of the proposed solutions.

Contextual Notes

There are limitations in the code related to variable data types and potential logical errors in the summation loop. The discussion does not resolve these issues definitively.

Who May Find This Useful

Individuals interested in C programming, particularly those learning about data types, function implementation, and debugging techniques.

konvictx
Messages
1
Reaction score
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
What's it returning?
 
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)
 
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.
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 21 ·
Replies
21
Views
9K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K