How to set a matrix to 0? [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 8 years ago.
Improve this question
I'm sorry if the question isn't really explanatory. I'm trying to set to 0 that matrix and thanks to the test printf I see that when i is 2 j is set to 0 insted of 3. Where am i wrong?
int matrix[3][3];
int i, j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
matrix[i][j] = 0;
printf("%d\t%d\t%d\n", matrix[i][j], i, j);
}
}

If you just want to zero out all the entries, you can use this:
memset(matrix, 0, sizeof(matrix));
You will have to #include <string.h>. If the size of the matrix isn't known at compile time by the compiler (it is declared elsewhere), then you can still use this:
memset(matrix, 0, rows*cols*sizeof(double));
In your case
memset(matrix, 0, 3*3*sizeof(double));

matrix[3][3] means its index is 0 , 1 and 2
therefore change this
for(i=0;i<4;i++){
for(j=0;j<4;j++){
to
for(i=0;i<3;i++){
for(j=0;j<3;j++){

Your loop is accessing uninitialized memory location, for eg, matrix[3][3]. It has indeterminate value and finally invokes undefined behavior.
Change
for(i=0;i<4;i++)
to
for(i = 0; i < 3; i++)
Do same the second loop.
You cam initialize your array to zero by using initializer as
int matrix[3][3] = {{0}};

Related

Why different behaviour of same code by different compilers? [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 2 years ago.
Improve this question
This is code written by me where I have to print a single integer denoting the minimum possible capacity of a tram (0 is allowed). It's a problem from codeforces. The answer in CodeBlocks is showing 6 (the right answer) but in codeforces compiler I'm getting another output.
Why is this happening?
#include <stdio.h>
int main() {
int n, i, j, max = 0, sum = 0;
int pssnger_left;
scanf("%d", &n);
int a[n][2];
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &a[i][j]); // declaring the value of array
}
}
pssnger_left = a[0][0] + a[0][1];
for (i = 1; i < n; i++) {
sum = pssnger_left - a[i][0];
sum = sum + a[i][i];
pssnger_left = sum;
if (max < sum)
max = sum;
}
printf("%d", max);
}
Input:
4
0 3
2 5
4 2
4 0
Output:
4221555
Answer:
6
Checker Log
wrong answer expected 6, found 4221555
Here is the link of the problem: https://codeforces.com/problemset/problem/116/A
Different compiler, different answer, 99% of the cases is explained by Undefined Behaviour, UB.
In the shown code here it is a[i][i];.
For any i > 1 that is not what you want it to be
and for most high i it illegally accesses beyond a.
-> Undefined Behaviour.
A hint on how I spotted this:
Whenever I see [i][i], actually whenever I see [same][same],
I think "diagonal in a square". And in your code I immediatly thought "What square?", because when seeing int a[n][2]; I thought "Long narrow table." and got a conflict of shapes there.
The problem is not basically with the compiler.
You are doing
sum = sum + a[i][i];
but you have declared a 2D array of n x 2 i.e. int a[n][2]
see in some compilers it shows out of bound because you are accessing an element which is not there in the array
and in compilers it just loops in the already existing array for ex if your array has 5 elements and you are trying to access 6th element then it will go back to 1st index,
so this might be whats happening here.

Loading an array with the squares of it's subscripts 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 4 years ago.
Improve this question
#define N 500
int subscriptSquares[N];
void loadArray(int nums[], int size)
{
for(int i = 0; i <= (size - 1); i++)
{
nums[i] = i * i;
printf("%i\n", nums[i]);
}
}
What I'm trying to do more specifically is load an array of size 500 with its subscript squares (so subscriptSquares = {0, 1, 4, 9, 16, 25, ...250000}). My problem is somewhere along the line the values are shortened. The last value in the array actually becomes 249001 instead of 250000 and so on so forth. I thought it had something to with the data type so I switched it to double but ended up with same values with the obvious annoyance of decimals. int subscriptSquares[N] is inside the main function. If you need more information let me know
In your code you have defined an array of size 500. It will contain indices from 0 to 499.
So, the last result of your calculation will be 499*499 = 249001.
If you want to get the result of 500*500 then you have to declare an array of 501 locations. The below code shows how.
#define N 501
int subscriptSquares[N];
void loadArray(int nums[], int size)
{
for(int i = 0; i <= (size - 1); i++)
{
nums[i] = i * i;
printf("%i\n", nums[i]);
}
}

Fill a array with numbers from 0-100 [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
I'm trying to fill an array with integers 0-100.
Here's my code:
int main()
{
int a[100];
for(int i = 0; i < a; i++)
{
a[i] = i;
printf("\n%d\n", a[i]);
}
}
I get this error:
comparison between pointer and integer (int and int) over the for line.
I can't figure it out. Any thoughts?
The cancel condition in your for loop is wrong. i<a You are comparing an int i variable with the pointer a that points to the memory location of the array. You would need to calculate the length of the array:
for(int i=0; i<sizeof(a)/sizeof(int); i++) {
But you could have found this solution in this 9 year old answer
Yes you are comparing pointer with an integer. This is why the error.
for(size_t i=0; i<sizeof(a)/sizeof(a[0]); i++) {
is what you wanted it to be and can be done in C language. Remember that sizeof operator results in value in size_t.
If you are not aware what that sizeof is doing - it is basically total number of bytes that the array object has - divided by each element's size - resulting in number of elements.
In case you are thinking from where did that pointer come?
Note one thing here a is converted into pointer (array decaying)to the first element - so it is nothing other than a pointer to the first element.
This should work for you,
int main()
{
int a[100];
int n = sizeof(a) / sizeof(a[0]); //Get the size of the array
for(int i=0; i<n; i++) { // loop through each elements of the array
a[i] = i;
printf("\n%d\n", a[i]);
}
}

I guess there is some mistake in the function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I would appreciate if anyone can help me figuring out why is this fuction (fn) does not print the int array elements?
#include <stdio.h>
void fn (int arr[], int s);
int main(void)
{
int arr[] = {5, 2, 0, 9, 7}, s=5;
fn (arr, s);
}
fn(int arr[], int s)
{
int i;
for (i = 0; i < 5; i++);
{
arr[i]++;
printf("%d ", arr[i] );
}
}
for (i = 0; i < 5; i++);
The semicolon at the end of the expression terminates the for-loop before even entering the body of the loop. Hence the body is seen as a stand-alone block of code by the compiler, unrelated to the for-loop.
Since the body of the loop is detached from the for-loop, variable i (the counter of the for-loop) is out of scope since i is local to the for-loop, and can only be called inside it as it was declared inside the conditional statement of the for-loop.
To fix this problem, remove the semicolon at the end of the for-loop condition.

for loop in c not outputting full array [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
this is a simple question as i'm a new-comer to C. I am trying to write a script for outputting an array of the tangents of radians, of multiples of 5 from 0-60. but for some reason the for loop i have written only does this for the first element, and all other elements in the resulting array are 0.00, and it wont print them for each loop. i'm sure i've done something simple wrong with my loop, but i just can't see it.
#include <stdio.h>
#include <math.h>
float rad(float degree){
return degree*M_PI/180;
}
int main(void){
int i, j, dim=13;
float Tan[dim];
for(i=0; i<13; i++);{
j+=5;
Tan[i]=tan(rad(j));
printf("%f\n", Tan[i]);
}
return 0;
}
First of all, in your code
j+=5;
is undefined behavior, as the intial value of j is indeterminate. To elaborate, j is an automatic local variable and not initialized explicitly, so the content is indeterminate.
Then, the for loop is also buggy.
for(i=0; i<13; i++);
should be
for(i=0; i<13; i++) // no ; here
to have a meaningful loop body to be executed.
1. You have inserted a semi-colon which you shouldn't have. Change your loop to :
for(i = 0; i < 13; i++){ //erase the ; after the parenthesis
j+=5;
Tan[i] = tan(rad(j));
printf("%f\n", Tan[i]);
}
2. Initialize variable j before trying to increase it with the statement j+=5, as this will lead to undefined behaviour.
There are two problems in your code :-
1) You haven't initialized j here int i, j, dim=13;
2) The way you used for loop is as per your requirement.Remove semicolon from the for loop statement.

Resources