rzrgenesys187
November 30th, 2007, 02:56 AM
We have to do a program for my engineering class where they want us to return an array from a function to the main function. I have no idea how to do this without pointers (which is what they want). Any help would be appreciated.
#include <stdio.h>
double read_datafile();
int main()
{
double temp_var[30][30], size1[2], size2[2];
int i, j;
temp_var = read_datafile(); // Calls the read_datafile function
size1[0] = temp_var[0][0];
size1[1] = temp_var[0][1];
double matrix1[(int)size1[0]][(int)size1[1]];
for (i = 0; i <= size1[0] - 1; i++)
{
for (j = 0; j <= size1[1] - 1; j++)
{
matrix1[i][j] = temp_var[i + 1][j];
}
}
temp_var = read_datafile();
size2[0] = temp_var[0][0];
size2[1] = temp_var[0][1];
double matrix2[(int)size2[0]][(int)size2[1]];
for (i = 0; i <= size2[0] - 1; i++)
{
for (j = 0; j <= size2[1] - 1; j++)
{
matrix2[i][j] = temp_var[i + 1][j];
}
}
}
double read_datafile() //Reads in the size of the matrix and the matrix itself. Returns this to the main function
{
char filename[30];
FILE *input;
double rows, columns;
double matrix[30][30]; // Declares the matrix variable
double return_var[30][30]; // Declares the variable to be returned
printf("What is the name of the file the matrix is in? ");
scanf("%s", filename);
input = fopen(filename, "r");
fscanf(input, "%lf %lf", &rows, &columns);
return_var[0][0] = rows;
return_var[0][1] = columns;
for (int i = 0; i <= rows -1; i++)
{
for (int j = 0; j <= columns - 1; j++)
{
fscanf(input, "%lf", &matrix[i][j]);
return_var[i + 1][j] = matrix[i][j];
}
}
return(return_var);
}
the errors i get are
Assignment12.cpp: In function ‘int main()’:
Assignment12.cpp:10: error: incompatible types in assignment of ‘double’ to ‘double [30][30]’
Assignment12.cpp:22: error: incompatible types in assignment of ‘double’ to ‘double [30][30]’
Assignment12.cpp: In function ‘double read_datafile()’:
Assignment12.cpp: error: cannot convert ‘double (*)[30]’ to ‘double’ in retur
#include <stdio.h>
double read_datafile();
int main()
{
double temp_var[30][30], size1[2], size2[2];
int i, j;
temp_var = read_datafile(); // Calls the read_datafile function
size1[0] = temp_var[0][0];
size1[1] = temp_var[0][1];
double matrix1[(int)size1[0]][(int)size1[1]];
for (i = 0; i <= size1[0] - 1; i++)
{
for (j = 0; j <= size1[1] - 1; j++)
{
matrix1[i][j] = temp_var[i + 1][j];
}
}
temp_var = read_datafile();
size2[0] = temp_var[0][0];
size2[1] = temp_var[0][1];
double matrix2[(int)size2[0]][(int)size2[1]];
for (i = 0; i <= size2[0] - 1; i++)
{
for (j = 0; j <= size2[1] - 1; j++)
{
matrix2[i][j] = temp_var[i + 1][j];
}
}
}
double read_datafile() //Reads in the size of the matrix and the matrix itself. Returns this to the main function
{
char filename[30];
FILE *input;
double rows, columns;
double matrix[30][30]; // Declares the matrix variable
double return_var[30][30]; // Declares the variable to be returned
printf("What is the name of the file the matrix is in? ");
scanf("%s", filename);
input = fopen(filename, "r");
fscanf(input, "%lf %lf", &rows, &columns);
return_var[0][0] = rows;
return_var[0][1] = columns;
for (int i = 0; i <= rows -1; i++)
{
for (int j = 0; j <= columns - 1; j++)
{
fscanf(input, "%lf", &matrix[i][j]);
return_var[i + 1][j] = matrix[i][j];
}
}
return(return_var);
}
the errors i get are
Assignment12.cpp: In function ‘int main()’:
Assignment12.cpp:10: error: incompatible types in assignment of ‘double’ to ‘double [30][30]’
Assignment12.cpp:22: error: incompatible types in assignment of ‘double’ to ‘double [30][30]’
Assignment12.cpp: In function ‘double read_datafile()’:
Assignment12.cpp: error: cannot convert ‘double (*)[30]’ to ‘double’ in retur