1
#include <stdio.h>
#include <string.h>

typedef struct arrays {

    char air[28];
    char water[28];
    char land[28];
    char c9[28];
    char c11[28];
    char c13[28];
}arr;

int main()
{
    FILE* fp = fopen("ass6file.csv", "r");
    if (!fp)  {
        printf("Can not open file");
        return 0;
    }
    char buff[1024];
    int row = 0;
    int col = 0;
    int i = 0;
    arr value[999];

    while (fgets(buff, 1024, fp))  {
        col = 0;
        row++;
        if (row == 1)
            continue;

        char* column = strtok(buff, ",");
        while (column)  {
            if (col == 1)
                strcpy(value[i].air, column);
            if (col == 2)
                strcpy(value[i].water, column);
            if (col == 3)
                strcpy(value[i].land, column);
            if (col == 4)
                strcpy(value[i].c9, column);
            if (col == 5)
                strcpy(value[i].c11, column);
            if (col == 6)
                strcpy(value[i].c13, column);
            column = strtok(NULL, ",");
            col++;
        }
        i++;
    }
    fclose(fp);
    int sum1[28];
    /*char sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,k;
    int var1,var2,var3,var4,var5,var6;
    int m1,m2,m3,m4,m5,m6;
    int s1=0,s2=0,s3=0,s4=0,s5=0,s6=0;*/
    for (int k = 0; k < 28; k++)
        sum1[k] = (int)value[k].air;
    for (int k = 0; k < 28; k++)
        printf("%d\n", sum1[k]);

    return 0;
}

The warning I am getting is:

filh.c: In function ‘main’:

filh.c:62:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   62 |  sum1[k]= (int)value[k].air;
      |           

I am new to C. Can anyone please help its printing big numbers that are not in the file?

like--

1966677344

1966677512

1966677680

1966677848

1966678016

1966678184

1966678352

1966678520

1966678688

1966678856

1966679024

1966679192

1966679360

1966679528

1966679696

1966679864

1966680032

1966680200

1966680368

1966680536

1966680704

1966680872

1966681040

1966681208

1966681376

1966681544

1966681712

1966681880
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Ayushman
  • 11
  • 5

1 Answers1

2

You are reading in columns from the .csv file as char arrays.

But, when doing your sum, you [probably] want to convert them to an int.

Just casting them to int (e.g. using (int)) won't work.

When you say: arr[i].air this is a pointer to the character data stored in the air field.

Serendipitously, the compiler flagged this because you're on a 64 bit machine and the size of an int variable is 32 bits but a pointer is 64 bits

To convert a string representation of a number to an actual number (e.g. we want an int value), we need to use the atoi [or strtol] function instead of casting.

Change:

sum1[k] = (int) value[k].air;

Into:

sum1[k] = atoi(value[k].air);

UPDATE:

i wanna perform arithmatic operations on the elements of array , find mean, variance etc of the respective columns, file is related to data science

Okay. So, I assume that all input data are numbers. We can refactor the struct to use integers. This means we can [using atoi et. al.] store the data as numbers once and then operate on it multiple times without having to reconvert it for each operation.

I assume that the input values are integers. If some/all of them can be fractional (e.g. 4.37), we can define INPUT_IS_FLOAT in the example code below.

your program works but its converting numbers with decimals to integers that will not give accurate results to me as i wanna calculate mean and other things and the data wont remain accurate

That is easily solved by using floating point variables for the sum, mean, variance, etc. This will work regardless if the input data is stored as an integer or floating point number

Here's some refactored code to compute the sum and mean of the air column. You can add similar code for the other fields/columns. Or, whatever values you need to compute:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef INPUT_IS_FLOAT
typedef double data_t;
#else
typedef long long data_t;
#endif

typedef struct arrays {
    data_t air;
    data_t water;
    data_t land;
    data_t c9;
    data_t c11;
    data_t c13;
} arr;

int
main()
{
    FILE *fp = fopen("ass6file.csv", "r");

    if (! fp) {
        printf("Can not open file");
        return 0;
    }

    char buff[1024];

    int count = 0;
    int col;
    arr value[999];

    arr *acur;

    // skip over column headers
    fgets(buff, sizeof(buff), fp);

    while (fgets(buff, sizeof(buff), fp)) {
        acur = &value[count++];

        col = 0;
        char *column = strtok(buff, ",");

        while (column) {
#ifdef INPUT_IS_FLOAT
            data_t curval = strtod(column,NULL,10);
#else
            data_t curval = strtoll(column,NULL,10);
#endif

            switch (col) {
            case 1:
                acur->air = curval;
                break;
            case 2:
                acur->water = curval;
                break;
            case 3:
                acur->land = curval;
                break;
            case 4:
                acur->c9 = curval;
                break;
            case 5:
                acur->c11 = curval;
                break;
            case 6:
                acur->c13 = curval;
                break;
            }

            column = strtok(NULL, ",");
            col++;
        }
    }
    fclose(fp);

    // sum all 'air' elements
    double sum = 0;
    for (acur = &value[0];  acur < &value[count];  ++acur)
        sum += acur->air;
    printf("sum of air: %g\n",sum);

    double mean = sum / count;
    printf("mean of air: %g\n",mean);

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • This solves your immediate problem. But, what do you want to do? You store data into the `sum1` array but you don't actually _sum_ it. With the corrected code, you would merely print the `air` values for each record. Please _edit_ your question and describe more of what you want to do with the data. After that, you can send me a comment here and I can update my answer to help further. – Craig Estey May 13 '21 at 15:42
  • You made a far better job of answering this than I did, and you managed to use *serendipitously*. +1. – Adrian Mole May 13 '21 at 15:50
  • @AdrianMole Actually, I was commenting under your answer about the `atoi` and other things for you to add, but you were too quick on the "delete trigger" ;-) – Craig Estey May 13 '21 at 15:52
  • @CraigEstey i wanna perform arithmatic operations on the elements of array , find mean, variance etc of the respective columns, file is related to data science – Ayushman May 13 '21 at 17:01
  • @CraigEstey your program works but its converting numbers with decimals to integers that will not give accurate results to me as i wanna calculate mean and other things and the data wont remain accurate – Ayushman May 13 '21 at 17:08
  • @AdrianMole although craig's code works well for me and i have understood it , i would still really like to see yours , it seems like you deleted your answer , my professor always says u should know multiple ways for same output, if possible can u please post that answer again? – Ayushman May 13 '21 at 19:20
  • 1
    I know Adrian from prior questions on SO [and he's a very competent programmer]. With sufficient privilege [which I have], I can [still] see his answer. He deleted it because he _just_ fixed the code to get rid of the warning by changing the types from `int` to `intptr_t`, so the compiler wouldn't complain about the cast. (e.g.) He changed two lines: `intptr_t sum1[28]; sum1[k] = (intptr_t)value[k].air;` He then realized that using `atoi` was needed instead. But, for him to fix his answer was a total rewrite. – Craig Estey May 13 '21 at 19:34
  • @CraigEstey I couldn't put it better myself, even if I tried! I 'fell into the trap' of posting a quick-fix answer, then realized that my answer wasn't helpful ... so I deleted it. – Adrian Mole May 13 '21 at 19:42