C45M99S C45M99S - 1 month ago 5
C Question

Sum of array in C

I'm working on a small program for school and can't get my array of doubles to sum properly. The specific error I'm getting is

warning C4244: 'return': conversion from 'double' to 'int', possible loss of data


on the line where sum is returned. And the sum displayed is gibberish.

The code is intended to:


  1. fill an array of doubles with user input,

  2. print the doubles on the screen in a column,

  3. add up all the doubles in the array, and

  4. print the sum onto the screen.



Code



#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#define MAX_SIZE 15

void FillArray(double a[], int *i);
void PrintArray(double a[], int i);
SumArray(double a[], int *i);
int main()
{
double input[15];
int input_size;
double sum;


FillArray(input, &input_size);
PrintArray(input, input_size);
sum = SumArray(input, &input_size);
printf("The sum is %f\n", sum);
return 0;

}

void FillArray(double a[], int *i)
{
int k;
printf("Filling an array of doubles\n");
printf("How many doubles do you want to enter (<15)\n");
scanf(" %d", i);

for (k = 0; k <*i; k++)
{
printf("Enter double:\n");
scanf("%lf", &a[k]);
}
}

void PrintArray(double a[], int i)
{
int k;
printf("Printing an array of integers:\n");
for (k = 0; k<i; k++)
{
printf("%f\n", a[k]);
}
printf("\n");
}

SumArray(double a[], int *i)
{
int k;
double sum = 0;
for (k = 0; k<*i; k++);
{
sum +=a[k];
}
return (sum) ;
}

cxw cxw
Answer

You need to specify double SumArray(...) instead of merely SumArray(...) where you declare and define the function. If you do not specify a return type, int is assumed. Specifically:

  void FillArray(double a[], int *i); 
  void PrintArray(double a[], int i);  
  double SumArray(double a[], int *i);        
/*^^^^^^-- add return type*/
  int main()

and

  double SumArray(double a[], const int numElements)
/*^^^^^^- same deal*/ /* also ^^^^^     ^^^^^^^^^^^ */

  {
      int k;
      double sum = 0.0; /* Edit 3: 0.0 rather than 0 for clarity */
      for (k = 0; k < numElements; ++k) /* no ; here!  --- Edit 3: ++k for speed and good practice */
      {            /* ^^^^^^^^^^^ */
          sum +=a[k];
      }
      return (sum) ;
  }

Edit Also, you can use const int numElements instead of int *i in SumArray. You don't need to modify the value inside SumArray, so you don't need the * and you can specify const. And it's a good practice to give your variables descriptive names, e.g., numElements instead of i. That will help you understand your own code when you have to maintain it later! (Ask me how I know. ;) )

To use this, you also need to change the call in main to remove the &:

sum = SumArray(input, input_size);
                   /* ^ no & here */

Edit 2 As @BLUEPIXY pointed out, the trailing ; on the for loop was misplaced. As a result, the {} block ran once, after the loop had completed. That would be a significant cause of the "gibberish" you saw: the effect was to set k=numElements and then set sum=a[numElements], which was a non-existent element. So the sum was being set to whatever random memory contents happened to be after a.

Comments