Reviewing CSV to C++ Code

When trying to read the following CSV file containing a 5x5 array as follows:
66.998 83.327 24.944 25.206 43.19
80.617 84.18 51.185 33.755 5.367
28.443 46.589 87.937 31.928 29.565
12.655 57.208 29.202 84.022 41.693
66.215 31.737 3.389 64.685 80.479

The code does not seem to actually read the file and gives random values as an output to the wanted output of the max, min, mean, and range. Is anyone able to see a mistake in my code which follows:

<#include <stdio.h>
#include <iostream>

using namespace std;

const int row = 5; // what dimension array are we using? the .csv
const int col = 5;

int main()
{
int input[row][col];
int range=0;
int mean=0;
int sum = 0;
int MaxVal=0;

FILE *file;
file = fopen("projectcsv.csv", "r"); // Replace with actual file name to be used.
if (file!=NULL)
{
fputs ("projectcsv.csv",file);
int i , j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
fscanf(file, "%d", &input[i][j]);
}
} // now we need to organize array elements

int MAX = input[0][0];
int MIN = input[0][0];

for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (input[i][j] > MAX)
MAX = input[i][j];
else if (input[i][j] < MIN)
MIN = input[i][j];

sum = sum + input[i][j];
}
}

mean = sum / 25;
range = MAX - MIN;

cout << "The largest of the values from the .csv file, as an int, was: " << MAX << endl;
cout << "The smallest of the values for the .csv file, as an int, was: " << MIN << endl;
cout << "The mean of the data read from the .csv file, as an int, was: " << mean << endl;
cout << "The range of the data read from the .csv file, as an int, was: " << range << endl;

fclose (file);
}
return(0);

}
>





The output being given currently is:


The largest of the values from the .csv file, as an int, was: 12587824
The smallest of the values for the .csv file, as an int, was: -1
The mean of the data read from the .csv file, as an int, was: 1514638
The range of the data read from the .csv file, as an int, was: 12587825

























Last edited on
You do appear to be trying to read doubles into ints.
Topic archived. No new replies allowed.