### arrays

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]);

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 figure out why i couldn't get the desired output???tq,,,
Well, I'll bite.

This clearly isn't C++.

Nevertheless, I also speak C, so bare with me.

Firstly, there is what appears to be an array parameter in your function prototype for 'normalize'. `int input[2][1]`

This represents a 2D array size 2 in one dimension and size 1 in the other. I'm sure you didn't mean to do this, as this is basically exactly the same as a 1D array size 2, since there's only one possible value for the second index. Somehow this misinterpretation is likely linked to other errors in the program.

You probably want to know how to scale a multi-dimensional array from another function (as a parameter, presumably). Well, in short, you basically can't in C unless you know the size of each array dimension except the first (I think, I never rely on this).

In long, yes you can! You just have to remember a multi-dimensional array is secretly a 1d array that contains all the elements in this kind of order: [0][0], [0][1], [0][2], [0][3], [1][0], [1][1], [1][2], .....

How does this help? Instead of trying to pass the array, pass the address of the first element along with the width and height (for 2D), and then treat the pointer like the start of a 1D array, accessing elements with [i + j*width]. I hope you understand, it would be reprimandable to elaborate further.

Also the function 'normalize' is clearly attempting to find the minimum, not normalize. Correct naming is really important in C and C++, for your own sake and other's!
Pls help me..!!!

[#include<stdio.h>
#include<stdlib.h>

float normalize (float c, int coord1);

int main()
{
int i,j, coord;
float points[40][3];
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]);
}
}
printf ("The highest absolute value is %.2f", answer);

for( i = 0; i < coord; i++)
{
printf("Scaled:\n");

for( j = 0; j < 3; j++)
{
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
1) Please use code tags when posting code here, so that we can read it clearly. Without being able to see the line numbers, it's harder for us to identify where the problem is.

2) In your normalise function, you define c to be a float, but then you try and use it like it's a 2-dimensional array. This is obviously wrong.

3) Why are you calling the function "normalise", when it's not normalising anything?
Last edited on
Topic archived. No new replies allowed.