Special Matrix, why doesnt work?? Please somebody help

Write a C program that prepares an m×n matrix of integers whose first line
contains integers from 1 to n, second line contains squares of the first line, third line
contains the cubes of the first line, and so on. m and n must be read from the keyboard.

I wrote a program and I got these outputs which is ;
0 1 2 3
1 1 4 9
1 1 8 27
1 1 16 81
1 1 32 243

but there is something wrong, I dont know where am I doing wrong!!

I should get this output ;
1 2 3 4
1 4 9 16
1 8 27 64
1 16 81 256
1 32 243 1024

These are my codes, can anybody say where error is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
  int Power(int j, int i){
     int partialResult;
		if (i == 0) return j;
        else if (j == 0) return 1;
	
		
     partialResult = Power(j, i-1);
     return partialResult*j;
  } 
int main(){
	int i=1,j=1;
	int matris [5][4];
	for(i=0;i<5;i++){//satırlar için 
		for(j=0;j<4;j++){//kolonlar için 
			matris [i][j]=Power(j,i);
		}}
		for(i=0;i<5;i++){
			for(j=0;j<4;j++){
				printf("%d\t",matris[i][j]);
			}
		printf("\n");
		}}
Your Power function is not correct.

Another reason the output differ is that you start at 0 instead of 1.
Last edited on
My (draft) code (reference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
int main(){
	
int i=1,j=1;
int matris [5][4];

for(j=0;j<4;j++)matris[0][j] = j + 1; //Initiate

for(i=0;i<5;i++){

if(i != 0){ //Skip level 0 (Because it's initiated)
for(j=0;j<4;j++)matris[i][j] = matris[i - 1][j]; //Copy the previous value from previous array
for(j=0;j<4;j++)matris[i][j] *= j + 1; }//Power up
}

for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d\t",matris[i][j]);
}
printf("\n");
}}

return 0;
}

(It should correct) :)

I will explain this later... Now I'm busy...
Last edited on
@Jacksion Marie

Fırst, thanks for this code, İt works, but I have to write this code with RECURSİVE FUNCTİONS. I checked out your work, it seems perfect but, I can not turnit in this work, I am even sure my lecture is gonna give me F , because he strongly said "USE RECURSİVE FUNCTİONS ".
@Peter87

I tried to fix, but couldn't work. Line 18 and 19, When I start with 1, İt is becoming [4][3] instead of [5][4]. And Can u please say which line is not correct, or can you say how you fix this line?
Missing a function in my draft?
I didnt understand when I first saw your code, I m still trying to figure out your code. You said you are going to explain later right?
???

for(j=0;j<4;j++)matris[0][j] = j + 1; //Initiate (1)
The first array should be initiated.
1 2 3 4
for(j=0;j<4;j++)matris[0][j] = j; //Initiate (2)
0 1 2 3

This result is different than the result you must have. Then (1) is correct

Start :
1
2
for(i=0;i<5;i++){
if(i != 0){ //Skip level 0 (Because it's initiated) 


for(j=0;j<4;j++)matris[i][j] = matris[i - 1][j]; //Copy the previous value from previous array

If i > 0, then copy the previous value from previous array.
for(j=0;j<4;j++)matris[i][j] *= j + 1; }//Power up
Every element in this column array (i) will be multiplied by row index (j). Do not forget "+ 1", this causes the first element will always be a value : 0
So ,test the code (matris[1][j] (i = 1))
Edit : Fixed missing output
1 2 3 4

matris[1][0] = 1 * (0 + 1) = 1 * 1 = 1
matris[1][1] = 2 * (1 + 1) = 2 * 2 = 4
matris[1][2] = 3 * (2 + 1) = 3 * 3 = 9
matris[1][3] = 4 * (3 + 1) = 4 * 4 = 16

Result : 1 4 9 16

i++;
Copy previous values (i = 2):
matris[2][0] = matris[2 - 1][0] = matris[1][0] = 1
matris[2][1] = matris[2 - 1][1] = matris[1][1] = 4
matris[2][2] = matris[2 - 1][2] = matris[1][2] = 9
matris[2][3] = matris[2 - 1][3] = matris[1][3] = 16

(matris[2][j] (i = 2))
1 4 9 16

matris[2][0] = 1 * (0 + 1) = 1 * 1 = 1
matris[2][1] = 4 * (1 + 1) = 4 * 2 = 8
matris[2][2] = 9 * (2 + 1) = 9 * 3 = 27
matris[2][3] = 16 * (3 + 1) = 16 * 4 = 64

And continue i++....

Do you understand? You should improve your code executing. If you need help, simply test the debug source (with variable logging)
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
#include<stdio.h>
int main(){
	
int i=1,j=1;
int matris [5][4];

for(j=0;j<4;j++)matris[0][j] = j + 1; //Initiate

for(i=0;i<5;i++){

if(i != 0){ //Skip level 0 (Because it's initiated)
for(j=0;j<4;j++){
printf("Before : %d\n", matris[i][j]);
matris[i][j] = matris[i - 1][j]; 
printf("After : %d\n", matris[i][j]);}

for(j=0;j<4;j++){
printf("Multiply : matris[%d][%d] (%d) *= j (%d) + 1 = %d\n", i, j, matris[i][j], j, matris[i][j] * (j + 1));
matris[i][j] *= j + 1;}

}//Power up
}

for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d\t",matris[i][j]);
}
printf("\n");
}}

return 0;
}


Hope this helps.

Last edited on
int i=1,j=1; ??? - Redundant

Back to your algorithm : m x n
m (i) - and n (j) - right ?

You should define some const variables (Such as m, n)
Don't specify specific values, such as : i = 5; j = 4;

Next, how to define them :
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
#include<stdio.h>
int main(){
//#define <name> <value>

#define m 5
#define n 4

int main(){
int matris [m][n];
int i,j;
for(j=0;j<n;j++)matris[0][j] = j + 1; //Initiate

for(i=0;i<m;i++){

if(i != 0){ //Skip level 0 (Because it's initiated)
for(j=0;j<n;j++)matris[i][j] = matris[i - 1][j]; //Copy the previous value from previous array
for(j=0;j<n;j++)matris[i][j] *= j + 1; }//Power up
}

for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d\t",matris[i][j]);
}
printf("\n");
}}

return 0;
}


Hope all goes well.
Last edited on
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
#include<stdio.h>
//Only Power() is changed
 int Power(int j, int i, int nValue = 0){
int partialResult;

if(!nValue){
if (i == 0) return j;
else if (j == 0) {return 1;} //You know, always 1

partialResult = j;
for(int k = 0;k < i - 1;k ++)
partialResult*=j;return Power(j,i, partialResult);}

else return nValue*j; //Called a trick :) (for "RECURSİVE FUNCTİONS" - ?!?!??!?)
} 


int main(){
	int i=1,j=1;
	int matris [5][4];
	for(i=0;i<5;i++){//satırlar için 
		for(j=0;j<4;j++){//kolonlar için 
			matris [i][j]=Power(j,i);
		}}
		for(i=0;i<5;i++){
			for(j=0;j<4;j++){
				printf("%d\t",matris[i][j]);
			}
		printf("\n");
		}}
Topic archived. No new replies allowed.