Reads from an external file called data2.dat note that the
name is in lower case. The file has two columns. The first column is
integers and the second is floating point. You are to create two
columns of numbers. The numbers in the columns are composed of the
numbers in the second column of data2.dat. If a zero is in column 1 of
data2.dat the corresponding number from column 2 is to go into the new
column 1. If a one is in column of data2.dat the corresponding number
from column 2 is to go into column 2 of the out put. Note that the
number corresponding with the first zero is to be the first output and
the number corresponding with the first 1 is to be the second output
on the same line as the first number. The total number of lines is the
largest of the total numbers of either ones or zeros in column one of
data2.dat. Note that if there are an unequal number zeros and ones
then you should fill with 0.0 for the column with the smaller number
of entries. Print both columns with a %5.lf format to the screen and
to an external file called out.dat.
Example from class
data.dat
0 7.3
0 -2.2
1 5.5
0 9.2
1 -2.2
Output from program
7.3 5.5
-2.2 -2.2
9.2 0.0
So I know we have to use arrays but this is what I have as my code. It will print out the list but it is all 0s.
Code:
/* homework 7 */
#include <stdio.h>
#include <math.h>
#define FILEIN "data2.dat"
#define FILEOUT "out.dat"
main()
{
/* Variables */
int col_1;
double col_2,x,y, num_data_pts=0;
FILE *list_1;
FILE *list_2;
/* Open external file data */
list_1=fopen(FILEIN,"r");
list_2=fopen(FILEOUT,"w");
/* Reading in the data */
while((fscanf(list_1,"%i %lf",&col_1,&col_2))==2)
{
if(col_1==0)x==col_2;
if(col_1==1)y==col_2;
printf("%5.lf %5.lf \n",x,y);
fprintf(list_2,"%5.lf %5.lf \n",x,y);
}
}