Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a 2D array from a 1D array 2

Status
Not open for further replies.

robinsql

Programmer
Aug 2, 2002
236
IE
I need to write a small function to populate a 2D array from the 1D array called array. My array needs to be in this format and the remaining values need to be calculated by dividing the value on the left by the corresponding value on the top i.e. element x will be 0.64/.99. Any ideas gratefully received. Thank you
1 0.99 0.64 121.42 4.65
0.99 x
0.64
121.42
4.65

#include <stdio.h>
#define DATA_FILE &quot;rates.txt&quot;
void get_data(double array[5]);

void main(void)
{
double array[5];

get_data(array);

}

/*sub-program*/
/*Fills the array with data from file q1new.txt */
void get_data (double array [5])
{
int i;
FILE *fp;
fp = fopen(DATA_FILE, &quot;r&quot;);
for (i=0; i < 5; ++i)
fscanf ( fp, &quot;%lf&quot;, &array );
fclose(fp);
}
}
 
Hello,

first you sould declare main to return an int value and to accept two parameters, as the line below shows:
int main(int argc, char *argv[])

Then you should lay back and think about the problem you have.

First you need a 2d array 5*5:
double array[5][5];

Then you can define a function doing the population. The following is a solution (though not the quickest variant):

void populate(double array[], double a2d[][5])
{
int i,j;
for(i = 0; i < 5; i++)
{
a2d[0] = array;
}
for(i = 0; i < 5; i++)
{
for(j = 1; j < 5; j++)
{
a2d[j] = a2d[j][0] / a2d[0];
}
}
}

You may notice, that you could save the first loop in populate, as the following code shows:

#include <stdio.h>
#define DATA_FILE &quot;rates.txt&quot;
void get_data(double array[]);
void present(double a2d[][5]);
void populate(double a2d[][5]);

int main(int argc, char *argv[])
{
double array[5][5];

get_data((double[])array);
populate(array);

present(array);
}

void present(double a2d[][5])
{
int i,j;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
printf(&quot;%f &quot;, a2d[j]);
}
printf(&quot;\n&quot;);
}
}

void populate(double a2d[][5])
{
int i,j;
for(i = 0; i < 5; i++)
{
for(j = 1; j < 5; j++)
{
a2d[j] = a2d[j][0] / a2d[0];
}
}
}

/*sub-program*/
/*Fills the array with data from file q1new.txt */
void get_data (double array [5])
{
int i;
FILE *fp;
fp = fopen(DATA_FILE, &quot;r&quot;);
for (i=0; i < 5; ++i)
fscanf ( fp, &quot;%lf&quot;, &array );
fclose(fp);
}
}

 
Sorry, had an mistace in may last post, here is correct verison:
#include <stdio.h>
#define DATA_FILE &quot;rates.txt&quot;
void get_data(double array[][5]);
void present(double a2d[][5]);
void populate(double a2d[][5]);

int main(int argc, char *argv[])
{
double array[5][5];

get_data(array);
populate(array);

present(array);
}

void present(double a2d[][5])
{
int i,j;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
printf(&quot;%f &quot;, a2d[j]);
}
printf(&quot;\n&quot;);
}
}

void populate(double a2d[][5])
{
int i,j;
for(i = 0; i < 5; i++)
{
for(j = 1; j < 5; j++)
{
a2d[j] = a2d[j][0] / a2d[0];
}
}
}

/*sub-program*/
/*Fills the array with data from file q1new.txt */
void get_data (double array [][5])
{
array[0][0] = 1;
array[1][0] = 2;
array[2][0] = 3;
array[3][0] = 4;
array[4][0] = 5;
}
 
Hi,
Thanks for your help. Am I making the entire thing more dificult than I should be, as the numbers are originally coming from a text file called rates.txt. Is it possible to populate a 2D array directly from the text file with the incorporated calculations?
I am getting these four errors when I try your code. Any ideas?
1. cast to array type is illegal
2. 'get_data' : too few actual parameters
3. '/' : illegal, right operand has type 'double [5]'
4. syntax error : '}'

 
fro,
Always put code in
Code:
 and
tags.

robinsql,
On which lines are these errors occuring? //Daniel
 
If you are reading these values from a file directly
into an array(of whatever dimensions), you will run into
issues with resizing your array.
You will have to use initial magic guesses or use
dynamic allocation.
The array data structure in C is very limited in this
way.
A better solution involves linked lists.
 
A final offering on this thread since, IMO, you
will have to stretch what you know in order to
flexibly deal with file input from unknown sized
files.

First a one dimensional array method..you
can emulate a 2d array by reading the array in
increments of two and stepping back one to get
the index...
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
int s = 0, x = 0;
float *farr = NULL, *tmp;
float t;
char buf[50];
FILE *fd;
  
      if (argc == 2) {
         farr = malloc(3 * sizeof(float));
         if ( (fd = fopen(argv[1],&quot;r&quot;)) != NULL) {
              while ( (fgets(buf,50,fd)) != NULL) {
                      if (s >= 3) {
                         tmp = realloc(farr,(s + 1) * sizeof(float)); 
                         farr = tmp;
                      }
                      farr[s] = (float)atof(buf);
                      printf(&quot;Read %f at %d mem = %p\n&quot;, farr[s],s,&farr[s]);  
               s++;
               }
              fclose(fd);
          } else {
            printf(&quot;Error opening file: %s\n&quot;, argv[1]);
            return 1;
          }
      } else {
          printf(&quot;You must give the filename as the first argument\n&quot;);
          return 1;
      }

   
     for (x=0 ; x < s ; x++) {
         printf(&quot;Reading element %d: %f\n&quot;, x,farr[x]);
     }
printf(&quot;Freeing memory\n&quot;);
free(farr);
return 0;
}

The second method involves using a linked list..
which is simpler IMO, and you can use a method
which reads the list two elements at a time or
uses the indexes to get values and keys.

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


struct fpair {
float element;
int index;
struct fpair *nxt;
};

struct fpair *retFpair(int, char *);
void printList(struct fpair *);

int main(int argc, char **argv) {
int y = 1;
char buf[30];
struct fpair *phd,*ptmp,*pnxt;
FILE *fd;
             if (argc != 2) {
                printf(&quot;Error: Please give me the filename as argument\n&quot;);
                return 1;
             }

        
   
            if ( (fd = fopen(argv[1],&quot;r&quot;)) != NULL) {
                 while ( (fgets(buf,30,fd)) != NULL) {
                           pnxt = retFpair(y,buf);
                               if (y == 1) {
                                   phd = pnxt;
                                   ptmp = phd;
                                   y++;
                                   continue;
                               }
                            ptmp->nxt = pnxt;
                            ptmp = pnxt; 
                            y++;
                  }
            fclose(fd);
            } else {
              printf(&quot;Could not open %s\n&quot;, argv[1]);
              return 1;
            }

printList(phd);
return 0;
} 
                                              
                          
struct fpair *retFpair(int ind, char *mystr) {
struct fpair *retS = malloc(sizeof(struct fpair));

           if (retS) {
              retS->element = (float)atof(mystr);
              retS->index = ind;
              retS->nxt = NULL;
            }
return retS;
}               


void printList(struct fpair *hd) {
  
     while (hd != NULL) {
            printf(&quot;Value: %f, list index: %d, mem add of next %p\n&quot;, hd->element,hd->index,hd->nxt);
            hd = hd->nxt;
     }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top