Different declarations of matrix in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I made the following's declaration:
Implementation 1:
int m[3][3];
m[0][0] = 1;
m[0][1] = 4;
m[0][2] = 5;
m[1][0] = 5;
m[1][1] = 7;
m[1][2] = 6;
m[2][0] = 5;
m[2][1] = 8;
m[2][2] = 8;
Implementation 2:
int m[3][3];
int m = {
{1,4,5},
{5,7,8},
{5,8,8},
};
Implementation 1 works just fine, but Implementation 2 results in a wrong output in my code (which is too long to put here) and I wanted to use 2 because is more compact. Why is the result in my output being different?

For me, the following code works perfectly:
#include<stdio.h>
int main()
{
int m[3][3] = {
{1,4,5},
{5,7,8},
{5,8,8},
};
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d \n",m[i][j]);
}
}
return 0;
}
Output:
1
4
5
5
7
8
5
8
8

Related

Is my code wrong or it's somehow compiler's fault? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
stdout gives:--------------------------------------------------
Here are the results of all 3 students.--------------------------------------------------
Although I wanted it to display:--------------------------------------------------Here are the results of all 3 students:
--> 8.75
--> 9.25
--> 9.00 --------------------------------------------------
Here is the code:
#include <stdio.h>
int main(void) {
/*The grades of 3 students*/
int grades[3][4] = {{ 7,10,9,9 }, { 10,10,8,9 }, { 9,8,9,10 } };
float result1 = (grades[0][0] + grades[0][1] + grades[0][2] + grades[0][3])/4;
float result2 = (grades[1][0] + grades[1][1] + grades[1][2] + grades[1][3])/4;
float result3 = (grades[2][0] + grades[2][1] + grades[2][2] + grades[2][3])/4;
printf("Here are the results of all 3 students:\n");
/*I stored the results into an array so i can use for loop to display them faster*/
float a[3]= {result1,result2,result3};
/*Here i wanted to try to display the results using for loop*/
int i;
for (i = 0; i > 3; i++){
printf(" --> %.2f\n", a[i]);
}
return 0;
}
What's the problem with the output ?
The problem is here:
for (i = 0; i > 3; i++)
Try for (i = 0; i < 3; i++) (while i is less than 3, instead of while i is greater than 3, which is never...)

Why are the wrong array values being printed? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

Please explain the following C code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I compile and run the C code shown below, it generates the following:
Input:
#include <stdio.h>
int main()
{
int i, j;
int a, b;
for (j = 0; j <= 4; j+=2)
{
a = j;
b = 0;
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
printf("%d %d\n", a, b);
}
}
Output:
0 0
2 40
4 80
If anyone can tell me why the following input generates the above output, this would be much appreciated.
Hope you understand it with the simple trace table I draw.
This seems to be a basic C example showing arithmetic and printf statements.
It helps if you break down a problem like this into modules:
1) Execute the steps 2a and 2b, with j = 0, 2, 4, sequentially:
for (j = 0; j <= 4; j+=2)
2a) For each index of j, b = b + 2 * j * i (a = j here)
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
2b) printf("%d %d\n", a, b) is just printing out the values of j (since a is assigned the value of j) and b, where the calculations are done in step 2a.
Next time try to give the exact area where you are confused. Explaining something like this over chat is not easy. You have to break it down on your own really.

Simple for-loop countdown timer in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This is what the code should do:
“Lift off in T minus 5 4 3 2 1 Blast-off!”
When I run it, it just keeps printing ''Sum = 5'' forever.
Code:
int main(void) {
int sum = 5;
int i;
printf("Lift off in T minus\n");
for (i = 0; i < 5; i=i+i) {
sum = sum - i;
printf("sum = %d\n",sum);
}
printf("Blast-off",sum);
return 0;
for (i = 0; i < 5; i=i+i) { // use i = i+1
sum = sum - i; //sum-- or sum = sum -1
printf("sum = %d\n",sum);
}
As initially i=0, so
i=i+i; //will be zero always, no increment.
And
sum = sum -1;
otherwise
i = 0 =>sum = sum - i; // = 5 as i=0
i = 1 =>sum = sum - i; // = 4 as i=1
i = 2 =>sum = sum - i; // = 2 as i=2
i = 3 =>sum = sum - i; // = -1 as i=3
Why not run the loop backwards?
for (i = 5; i > 0; --i) {
printf("i = %d\n",i);
}
This is simpler so the potential for bugs to creep in is reduced. Also, your final printf if malformed: you're missing a format specifier for sum.
Your specific problem: replace i=i+i with i=i+1 or something similar. (I prefer ++i).

The int array displays an odd value at the end [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am running this code for teaching purposes about sizeof and displaying array values; everything looks OK with the char array (the last element of the array '0' or NULL is not displayed, the float array displays it as 0.0000... but the char array gives me the last element of 54417138 that looks like an address (hex equivalent 206F6554). I tried to retrieve the content of it assuming it is an address (see the comment part of the code) but got a stack overflow error so I am lost at this moment - any help will be appreciated.
#include <stdlib.h>
int main(void) {
int i =0;
int j = 0;
int k = 0;
char name[] = {'T','e','o',' ','G','u','g' };
int z = sizeof(name)/sizeof(name[0]);
printf("%d\n", z);
for ( k=0; k <= z; k++) {
printf("%c", name[k]);
}
for ( k=0; k <= z; k++) {
printf("%c", *(name+k));
}
printf("\n");
int number[] = {1,2,3,4,5,6,7,8 };
int x = sizeof(number)/sizeof(number[0]);
printf("%d\n", x);
for ( i=0; i <= x; i++) {
printf("%d\t", number[i]);
}
for ( i=0; i <= x; i++) {
printf("%d\t", *(number+i));
}
printf("\n");
/*int *ptr = 0x206F65540;
printf("Memory address is: %i\n", ptr);
printf("Content of that address is: %d\n", *ptr);
printf("\n");*/
float number1[] = {1.0, 2.5, 3.1234, 10.10};
int y = sizeof(number1)/sizeof(number1[0]);
printf("%d\n", y);
for (j=0; j <= y; j++) {
printf("%f\t", number1[j]);
}
for (j=0; j <= y; j++) {
printf("%f\t", *(number1+ j));
}
printf("\n");
return EXIT_SUCCESS;
}
This is the output of the program:
7
Teo Gug Teodor Gug
8
1 2 3 4 5 6 7 8 544171348 1 2 3 4 5 6 7 8 544171348
4
1.000000 2.500000 3.123400 10.100000 0.000000 1.000000 2.500000 3.123400 10.100000 0.000000
note the strange value in the display of int array of 544171348*
You are accessing beyond the end of your arrays with this kind of loop
for ( i=0; i <= x; i++) {
should be
for ( i=0; i < x; i++) {
^
^
as array indices are zero based.
Reading beyond the end of your arrays is undefined behaviour.

Resources