Thread: pass the array in functions and returning array value in main fuction in c

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    pass the array in functions and returning array value in main fuction in c

    hy i wrotea program to get temprature from user and calculate its average, highest and minimum temprature but it is giving a garbage value tell me whtx wronge with this program
    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);
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should definitely use indentation.

    Also, you're not checking user input to make sure they enter a valid number (that's actually a digit, and is under 15).

    Otherwise, unless I'm missing something, your program looks good. Maybe copy/paste the output you're getting?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your printf/scanf formats are mixed up, and you're using floats as array subscripts.
    Also, the indentation could be a lot better.
    Code:
    #include<stdio.h>
    //!!#include<conio.h>
    float average(float[], float);
    float highest(float[], float);
    float lowest(float[], float);
    int main(void)                  //!! fixed
    {
      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();
      return 0;                     //!! added
    }
    
    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);
    }
    Here are the errors that need to be fixed
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:13: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’
    bar.c: In function ‘lowest’:
    bar.c:53: error: array subscript is not an integer
    bar.c:54: error: array subscript is not an integer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    also
    Code:
      for (x = 0; x < n; n++)
    you want to increment x insted of n
    You do that in each of the functions.

    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Pass array through functions
    By willc0de4food in forum C Programming
    Replies: 13
    Last Post: 03-06-2005, 10:53 AM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread