Spare Matrix Function [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 5 years ago.
Improve this question
(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)
That's a problem for a sheet in out subject
here is my code:
#include <stdio.h>
#include <stdlib.h>
int Spare(int [4][5]);
int main()
{
int arr[4][5];
int m,n;
for(m=0;m<4;m++){
for(n=0;n<5;n++){
scanf("%d ",&arr[4][5]);
}
}
Spare(arr[4][5]);
return 0;
}
int Spare(int Arr[4][5]){
int i,j;
int zerocount=0;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
if(Arr[i][j]==0){
zerocount++;
}
}
}
if(zerocount>=10) return 1;
else return 0;
}
Its Running but after the user enter inputs of array it stops working!
Any Help Guys?

Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.
The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist.
The Spare function expects an array as parameter and you are passing the single element only.
The function call must be done in this way: Spare(arr)

Related

Guessing Game - Can't figure out why it produces a large negative integer [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
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment

prototypes are correct but garbage values are displayed in the output [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
The prototype (void example();) I have mentioned for the program is correct, but the compiler is giving garbage value instead of correct values in the output.
What codes should be added or removed?
Here is my code:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void example()
{
static int x = 10;
x=x+10;
printf("\n%d",&x);
}
int main()
{
int i;
for(i=0;i<=3;i++)
{
example();
}
getch();
return 0;
}
You are using an adress of a variable where printf wants just the value:
printf("\n%d",&x);
->
printf("\n%d",x);
Your result might also be improved by using "%d\n" instead.

What will be the result of next function and array in main? [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
today i tried to do something new,but i didn't do that correct.Would anyone be able to do that and explain why is it so like that? Thank you in advance
#include<stdio.h>
void function(int a[],int n)/*The definition of function with void type,with parameters
int a[],int n */
{
int i;// declared count,type integer//
for(i=0;i<n;i++)//count goes from 0,to <n,and increment all time while goes//
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
printf("\n");// printing the newline //
}
main()
{
int a[]={1,2,3,4,5,6,7}; // declaration of array with 7 elements //
int n=5;// declaration of variable n type integer value of 5 //
function(a,n) // calling the function with parametres a,n//
} // end of sequence //
In my case i got the result of the 1,2,3,4,because i tought that the count goes from 1,to the one number less than n=5,but the IDE show the result of 135 ,i think the problem in my way is with counter...but all advices are welcome,thanks
Please make sure you are posting properly formatted valid C code.
Note that what you get is not one hundred and thirty five, but one, three, and five. You get that because you are incrementing the loop counter twice.
Here's a working, more readable version:
#include <stdio.h>
void function(int a[],int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[]={1,2,3,4,5,6,7};
int n=5;
function(a,n);
return 0;
}
replace
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
with
printf("%d",a[i]);// printing on the screen integers (a[i],i=i+1)//
in your code you were incrementing i twice. Once in the while and once in the a[i++]

Summing elements of a 3d array 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
Im trying to sum the elements of a 3d array in C. The code recognizes that position check[1][1][0]=4 and adds 4 to sum when the loop reaches this position. However for the rest of the array it continues to add on this value again, and then it adds the total sum again for the rest of the positions of the array. Can anyone see why?
#include <stdio.h>
main() {
int check[3][3][3]={ 0 };
int size=2;
int i,j,k,sum=0;
check[1][1][0]=12;
for(k=0;k<size;k++) {
for(j=0;j<size;j++) {
for(i=0;i<size;i++) {
printf("i=%d, j=%d,k=%d, checkijk=%d ",i,j,k,check[i][j][k]);
sum+=sum+check[i][j][k];
printf("sum=%d\n", sum);
}
}
}
printf("The sum is %d\n",sum);
}
sum+=sum+check[i][j][k];
should be
sum+=check[k][j][i];
And if you want to sum all the values int size = 2; must be int size = 3;

Why won't a 5x5 multiplication array appear? [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 8 years ago.
Improve this question
I did this and I think it should work:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++);
{
for(j=1;j<=5;j++);
{
printf("%d",i*j);
}
}
return 0;
}
But it just prints out 36...
What am I doing wrong?
Remove the two extra ;s:
for(i=1;i<=5;i++);
^
{
for(j=1;j<=5;j++);
^
As others have pointed out, the semicolons before the opening braces of your for loops are preventing the printing.
Additionally, if you want the output to appear as a matrix, you'll want to printf a newline after your first for loop:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d",i*j);
}
printf("\n");
}
return 0;
}

Resources