I've started learning functions in C.
I have a task to convert numbers from imperial to metric, which are contained in the 2d Array.
Array:
{1300, 0} [0][0] [0][1]
{2198, 0} [1][0] [1][1]
{4199, 1} [2][0] [2][1]
{2103, 0} [3][0] [3][1]
{3104, 1} [4][0] [4][1]
{1093, 1} [5][0] [5][1]
{3204, 0} [6][0] [6][1]
So I am calling function with double return SortedArray[DATA_ROWS] = find_function(MixedData);
find_function logic:
0 is an indicator of the metric system value and 1 is an indicator of imperial system value,
i is a row indexer and j is a column indexer.
So when for cycles find out that column value is 0 or 1 they save [j-1](an actual value that we need) into SortedArray.
It also converts value if finds out that column value is 1 SortedArray[i] = MixedData[i][j-1]*CONSTANT;
Just for a check, I did printf to see if values are correct and correctly converted if needed.
I got this output:
1300.00
2198.00
1301.69
2103.00
962.24
338.83
3204.00
So, seems like correct, but in Task I have another task - use 2 functions to solve this task.
I've decided to make another function, which will sum all values in SortedArray[DATA_ROWS] and then calculate avg - average.
Then print them out.
And here comes the problem.
Calling function with no return total_AVG(SortedArray); with copied array SortedArray[DATA_ROWS]
Just to check made a printf and got this:
-1.#R
0.00
0.00
0.00
0.00
0.00
0.00
It seems like my SortedArray[DATA_ROWS] did not copy into the second function total_AVG
Pointers are coming in the next task, so according to task's timeline I can't use them * (and even if I could, I have no idea how to use them still)*
Can you please tell me, what I am doing wrong.
NB:
Some comments and variables, which are not used just old ones, when I tried to correct my code.
And also I only need to understand why SortedArray[DATA_ROWS] did not copy into second function total_AVG. All further logic will be corrected after solving this problem.
Thank you! And sorry for broken English!
CODE:
#include <stdio.h>
#define DATA_ROWS 7
#define DATA_COLS 2
#define CONSTANT 0.31
#define MAX 3
double find_function(int MixedData[DATA_ROWS][DATA_COLS]);
void total_AVG(double SortedArray[DATA_ROWS]);
int main(void)
{
int i;
double SortedArray[DATA_ROWS];
int MixedData[DATA_ROWS][DATA_COLS] = {
{1300, 0},//[0][0] [0][1]
{2198, 0},//[1][0] [1][1]
{4199, 1},//[2][0] [2][1]
{2103, 0},//[3][0] [3][1]
{3104, 1},//[4][0] [4][1]
{1093, 1},//[5][0] [5][1]
{3204, 0}};//[6][0] [6][1]
SortedArray[DATA_ROWS] = find_function(MixedData);
total_AVG(SortedArray);
return 0;
}
double find_function(int MixedData[DATA_ROWS][DATA_COLS])
{
// imperial numbers from source array "mixedData" are 4199,3104,1093;
int i,j; // indexers
double SortedArray[DATA_ROWS];
// 7 rows, each row suppose to contain only metric values
// That means, if second for cycle with j indexer finds 0, it will contain j-1 value for example 1300 in SortedArray
// If it finds in second for cycle with j indexer 1, it will converte the value from imperial to metric for example 4199*0.31
/* {1300, 0} [0][0] [0][1]
{2198, 0} [1][0] [1][1]
{4199, 1} [2][0] [2][1]
{2103, 0} [3][0] [3][1]
{3104, 1} [4][0] [4][1]
{1093, 1} [5][0] [5][1]
{3204, 0} [6][0] [6][1] */
// Probably problem in "double SortedArray and int MixedData"
for(i=0;i<DATA_ROWS;i++)
{
for(j=0;j<DATA_COLS;j++)
{
if(MixedData[i][j]==0)
{
SortedArray[i] = MixedData[i][j-1];
}
else if(MixedData[i][j]==1)
{
SortedArray[i] = MixedData[i][j-1]*CONSTANT;
}
}
}
for(i=0;i<DATA_ROWS;i++)
{
//total += SortedArray[i];
printf("%.2lf\n", SortedArray[i]);
}
return SortedArray[DATA_ROWS];
}
void total_AVG(double SortedArray[DATA_ROWS])
{
double avg,total;
int i;
for(i=0;i<DATA_ROWS;i++)
{
printf("%.2lf\n", SortedArray[i]);
}
//avg = total/DATA_ROWS;
//printf("Total by every worker: %.2lf\n",total);
//printf("In average by every worker: %.2lf", avg);
return;
}
SortedArray[i] = MixedData[i][j-1];you access invalid location whenj=0.SortedArray[DATA_ROWS] = find_function(MixedData);here too.return SortedArray[DATA_ROWS];here too.SortedArray[DATA_ROWS] = find_function(MixedData);->double result = find_function(MixedData);. Your find function should return a single double. Most of your confusion comes from your attempt to "return arrays". You can't do that in C. You need to pass arrays by pointers. I'd advise stepping back from 2D arrays and study how arrays decay to pointers, and how that happens when you pass an array as function parameter.SortedArray[i] = MixedData[i][j-1];you access invalid location whenj=0* I have if else cycle for chechking for imperial numbers marked as 1, when j=1 So if I am not mistaken when j = 0 I have for example 1300 value. And when j=1, it's either 0 or 1.ifit's a 0, just save previous value j-1 = j = 0, which is 1300 into arrayelse ifit's a 1, just save previous value j-1 = j = 0, which is 1300 into array, but first multiply byCONSTANT