2

I have file that looks like this:

01 01 5.00 1.50 7.50
02 01 4.00 3.00 12.00
02 02 3.00 4.00 12.00
03 01 4.50 3.00 13.50
03 01 7.50 2.50 18.75
03 01 6.00 0.50 3.00 
04 01 2.00 3.00 6.00 
04 02 2.00 3.00 6.00
05 01 1.50 3.00 4.50
07 01 5.00 1.00 5.00
09 01 1.50 6.00 9.00

I am trying to read each line and store each column of data into separate arrays. Something like this:

int A[100] = {1, 2, 2, 3, 3, 3, 4, 4, 5, 7, 9}
int B[100] = {1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}
double C[100] = {5.00, 4.00, 3.00, 4.50, 7.50, 6.00, 2.00, 2.00, 1.50, 5.00, 1.50}
double D[100] = {1.50, 3.00, 4.00, 3.00, 2.50, 0.50, 3.00, 3.00, 3.00, 1.00, 6.00}

So far I have this:

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

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE]

    while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
    }
    return 0;
}

My issue is how to do I store my columns of data in each array with the correct index? I also have been using input file redirection:

program.exe < txtfile.txt

This is because I want to be able to read any sort of text file with columns of data like the one above.

Bounty information, since it didn't look nice when i posted it:

Okay so I want to be able to see if the first column and second column have recurring digits at the same time. For example, in the first column, there are recurring '03' going down, and in the second column, there are recurring '01' going down. Both happen at the same stage, like this:

    03 01
    03 01
    03 01

I want to be able to recongize that and if that is true, I want to be able to sum the doubles on the fifth column(far right). I want it to look like this:

    01 01 5.00 1.50 7.50
    02 01 4.00 3.00 12.00
    02 02 3.00 4.00 12.00
    03 01 4.50 3.00  ---
    03 01 7.50 2.50  ---
    03 01 6.00 0.50 35.25 
    04 01 2.00 3.00 6.00 
    04 02 2.00 3.00 6.00
    05 01 1.50 3.00 4.50
    07 01 5.00 1.00 5.00
    09 01 1.50 6.00 9.00

other wise, I want the program to continue searching for the first/second column occurrence. A bit of extra background info, The first column is a room type, the second column is a room instance, and the fifth column is the total area of those rooms. This is why I want to sum the areas on the far right, because the room types are the same as the instances.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 1
    Why do you mean store in each array with index? – fluter Apr 17 '16 at 06:26
  • Yeah sorry, I think I just meant to say store each element in an array. – RoadRunner Apr 17 '16 at 06:29
  • 4
    Add an `i++` as the last statement inside the `while` loop? – kaylum Apr 17 '16 at 06:29
  • 2
    the posted code doesn't compile, at all. When compiling, always enable all the warnings, then fix those warnings. (for `gcc` at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99` ) – user3629249 Apr 17 '16 at 06:31
  • 3
    as @kaylum said, increment I in the loop, other parts of the code looks fine, you are already store them in the array. – fluter Apr 17 '16 at 06:32
  • 2
    for ease of readability and understanding by us humans: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* – user3629249 Apr 17 '16 at 06:34
  • Yeah thanks for the input guys. I'll look out for these things next time. I appreciate the constructive criticism. – RoadRunner Apr 17 '16 at 06:36

4 Answers4

2
#include <stdio.h>
#inlcude <stdlib.h>

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE]

    while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
        i++; // your saviour  
    }
    return 0;
}
2

the following code:

cleanly compiles
does not leave trash in unused entries in arrays
eliminates unneeded variables
checks to assure that arrays are not overflowed
removes ~1/2 the statements in the code as they are not needed
corrects the spelling of the word `include`

and now the code:

#include <stdio.h>
#include <stdlib.h> // size_t

#define MAXSIZE (100)

int main( void )
{
    int    A[MAXSIZE] = {0};
    int    B[MAXSIZE] = {0};
    double C[MAXSIZE] = {0.0};
    double D[MAXSIZE] = {0.0};

    for ( size_t i=0;
          i< MAXSIZE && (scanf("%d %d %lf %lf", 
                               &A[i], &B[i], &C[i], &D[i]) == 4);
          i++);

    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
2

Modifying the code from already approved answer to fit the requirement of the bounty -

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

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d, e;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE], E[MAXSIZE];

    while (scanf("%d %d %lf %lf %lf", &a, &b, &c, &d, &e) == 5) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
        if (i > 0 && A[i] == A[i-1] && B[i] == B[i-1])
        {
          E[i] = E[i-1] + e;
          E[i-1] = -1;
        }
        else
          E[i] = e;
        i++;  
    }
    for (int j = 0; j < i; j++)
    {
      printf("%d %d %.2lf %.2lf %.2lf\n", A[j], B[j], C[j], D[j], E[j]);
    }
    return 0;
}

For the given input it will print -

1 1 5.00 1.50 7.50
2 1 4.00 3.00 12.00
2 2 3.00 4.00 12.00
3 1 4.50 3.00 -1.00
3 1 7.50 2.50 -1.00
3 1 6.00 0.50 35.25
4 1 2.00 3.00 6.00
4 2 2.00 3.00 6.00
5 1 1.50 3.00 4.50
7 1 5.00 1.00 5.00
9 1 1.50 6.00 9.00
Sajib Mahmood
  • 3,382
  • 3
  • 37
  • 50
1

Not sure if I understood your question correctly but here's an idea:

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

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE];
int previousA=-1, previousB=-1;
double tempsum=0.0;

while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

    A[i] = a;
    B[i] = b;
    C[i] = c;
    D[i] = d;
    if (previousA==A[i] && previousB==B[i]) { //to check for recurrent values
        tempsum+=(C[i]*D[i]);
    }
    else {
        //do whatever you want with the last stored tempsum value, which is the result of the multiplication of the C and D elements needed
        tempsum=(C[i]*D[i]); //then tempsum is reset to the current multiplication
    }
    i++;
}
return 0;

}

DGounaris
  • 182
  • 9