arrays n functions

Question: Write a program that reads in a two-dimensional array representing the coordinates of points in space.
The program should determine the largest absolute value in the array and scale each element of the array by dividing it by this largest value.
The new array, which is called the normalized array, should have the largest value.
The program consists of two functions:
• main() – obtains the number of data points, and the x, y, and z coordinates of each point.
• normalize() – normalize the array.


[double normalize(int input[2][1], int row, int column);

int main()
{

int row1 = 2;
int column1 = 1;
int c[2][1];
double answer, scale1, scale2, scale3, scale4, scale5, scale6;

printf("This program is designed to determine the largest absolute value in a\n");
printf("two-dimensional array and scale each element of the array by dividing it\n");
printf("by this largest value.\n\n");

printf("Enter the coordinate of X1:");
scanf("%d",&c[0][0]);

printf("Enter the coordinate of X2:");
scanf("%d",&c[0][1]);

printf("Enter the coordinate of Y1:");
scanf("%d",&c[1][0]);

printf("Enter the coordinate of Y2:");
scanf("%d",&c[1][1]);

printf("Enter the coordinate of Z1:");
scanf("%d",&c[2][0]);

printf("Enter the coordinate of Z2:");
scanf("%d",&c[2][1]);

answer = normalize(c,row1,column1);

printf("The highest value is %.2f\n",answer);

scale1 = (c[0][0]) / answer;
scale2 = (c[0][1]) / answer;
scale3 = (c[1][0]) / answer;
scale4 = (c[1][1]) / answer;
scale5 = (c[2][0]) / answer;
scale6 = (c[2][1]) / answer;

printf("Scaled:\n");
printf("Coordinate of X1:%.2f\n", scale1);
printf("Coordinate of Y1:%.2f\n", scale2);
printf("Coordinate of Y1:%.2f\n", scale3);
printf("Coordinate of Y2:%.2f\n", scale4);
printf("Coordinate of Z1:%.2f\n", scale5);
printf("Coordinate of Z2:%.2f\n", scale6);

system("pause");
return 0;
}

double normalize(int input[2][1], int row, int column)

{
int k, l;
int highest = input[0][0];

for( k = 0; k < row; k++)
for( l = 0; l < column; l++)
if ( input[k][l] > highest)
highest = input[k][l];

return highest;
}

]

>>>could anyone pls help me to figure out why i couldn't get the desired output???tq,,,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<stdlib.h>

float normalize (float c, int coord1);

int main()
{
    int i,j, coord;
    float points[40][3];
    float answer;
    float scale;
    
    printf("This program is designed to determine the largest absolute value in a\n");
    printf("two-dimensional array and scale each element of the array by dividing it\n");
    printf("by this largest value.\n\n");
    printf("How many coordinates do you have? : ");
    scanf("%d", &coord);
    
    for(i = 0; i < coord; i++)
       {   
           printf("Enter coordinate %d <x y z> : ", i+1);
           
           for(j = 0; j < 3; j++)
           {
           scanf("%.2f", points[i][j]);
           }
       }
       answer = normalize(points[40][3], coord);
       printf ("The highest absolute value is %.2f", answer);
       
    for( i = 0; i < coord; i++)
    {
         printf("Scaled:\n");
         
         for( j = 0; j < 3; j++)
         {
         scale = (points[i][j]) / answer;
         printf("Coordinate of %d:%.2f\n", i, scale);
         }
    }
    
       system("pause");
       return 0;
}

float normalize (float c, int coord1)
{
       int k,l;
       float highest = c[0][0];
       
       for(k = 0; k < coord1; k++)
       {    
           for(l = 0; l < 3; l++)
           {
           if(c[k][l]) > highest)
           highest = c[k][l];
           }    
       }
       return highest;

}



>>>errors:
line 49 - invalid types `float[int]' for array subscript
line 55 - `float[int]' for array subscript
- expected primary-expression before '>' token
- expected `;' before ')' token
Last edited on
Use code tags so we can see what lines the errors correspond to.
Your function states it takes a float, not an array of floats, but on line 49 you are accessing it like it is an array.
i'd already edited the code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<stdlib.h>

float normalize (float c[][3], int coord1);

int main()
{
    int i,j, coord;
    float points[40][3];
    float answer;
    float scale;
    
    printf("This program is designed to determine the largest absolute value in a\n");
    printf("two-dimensional array and scale each element of the array by dividing it\n");
    printf("by this largest value.\n\n");
    printf("How many coordinates do you have? : ");
    scanf("%d", &coord);
    
    for(i = 0; i < coord; i++)
       {   
           printf("Enter coordinate %d <x y z> : ", i+1);
           
           for(j = 0; j < 3; j++)
           {
           scanf("%.2f", points[i][j]);
           }
       }
       answer = normalize(points[40][3], coord);
       printf ("The highest absolute value is %.2f", answer);
       
    for( i = 0; i < coord; i++)
    {
         printf("Scaled:\n");
         
         for( j = 0; j < 3; j++)
         {
         scale = (points[i][j]) / answer;
         printf("Coordinate of %d:%.2f\n", i, scale);
         }
    }
    
       system("pause");
       return 0;
}

float normalize (float c[][3], int coord1)
{
       int k,l;
       float highest = c[0][0];
       
       for(k = 0; k < coord1; k++)
       {    
           for(l = 0; l < 3; l++)
           {
           if(c[k][l] > highest)
           highest = c[k][l];
           }    
       }
       return highest;

}



errors:
line 28 :cannot convert `float' to `float (*)[3]' for argument `1' to `float
normalize(float (*)[3], int)'
the code can be compiled and run, but then it displays wrong output on normalisation part..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<stdlib.h>

float normalize (int c[][3], int coord1);

int main()
{
    int i,j, coord;
    int points[40][3];
    float answer;
    
    printf("This program is designed to determine the largest absolute value in a\n");
    printf("two-dimensional array and scale each element of the array by dividing it\n");
    printf("by this largest value.\n\n");
    printf("How many coordinates do you have? : ");
    scanf("%d", &coord);
    
    for(i = 0; i < coord; i++)
       {   
           printf("\nEnter coordinate %d <x y z> : ", i+1);
           
           for(j = 0; j < 3; j++)
           {
           scanf("%d", &points[i][j]);
           }
       }
       answer = normalize(points, coord);
        
    printf("\nThe normalisation of the coordinates is:-\n");
    for( i = 0; i < coord; i++)
    {
         printf("\nCoordinate %d: ", i+1);
         for( j = 0; j < 3; j++)
         {
         printf("%.2f\t", answer);
         }
         printf("\n");
    }
    printf("\n");
    
    system("pause");
    return 0;
    
}

float normalize (int c[][3], int coord1)
{
       int i,j;
       float largest = c[0][0];
       float scale;
       
       for(i = 0; i < coord1; i++)
       {    
           for(j = 0; j < 3; j++)
           {
           if(c[i][j] > largest)
           largest = c[i][j];
           }    
       }
       for(i = 0; i < coord1; i++)
       {    
           for(j = 0; j < 3; j++)
           {
           scale = (c[i][j]) / largest;
           }    
       }
       return scale;

}


Topic archived. No new replies allowed.