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 have little problem here. Ever since I used srand command I no longer get output I want.
I have to create program that will create array not matter how long.. I chose 5. Then it has to create random numbers into it and 5th number in it has to be 0 and then it has to count all five numbers together. I was sucessful and I did even use some printfs to check if arr[5] is zero. It is. In output I receieve this: 5random numbers (that ones that were created into array) 0 (to check if command where i set arr[5] really set it to zero and result which only count all 5random numbers together but without changing 5th arr into 0. Any ideas why? Thanks!
Code looks like this:
#include <stdio.h>
#include <time.h>
main()
{
srand(time(NULL));
int result,i;
int arr[5];
for (i=0;i<5;i++)
{
arr[i] = rand() % 10+1;
printf("%d ",arr[i]);
}
arr[5]=0;
printf("\n");
printf("%d\n",arr[5]);
i=0;
for(;i<5;i++)
{
result +=arr[i];
}
printf("%d\n",result);
printf("%d\n",arr[5]);
}
The declaration int arr[5] gives an array with five elements numbered from 0 to 4 inclusive. Accessing the 6th element, arr[5], is undefined.
If you want five elements and a "sentinel" (the zero marker), you could define arr as
int arr[6];
You should probably also initialize result to 0 otherwise its value is undefined since it's a local variable (in fact, the meaning of the whole program is, strictly speaking, undefined in this case).
Related
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 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.
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 1 year ago.
Improve this question
I have written a piece of c code which I intend to just print out the value of a 2d array
I'm very new to c so this answer might be really basic sorry.
my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.
heres my code
#include <stdio.h>
int main(void) {
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i2 = 0;
printf("hello");
for(int i = 0; i > 4; i++){
i2 = i;
printf("hi %d", array1[i][i2]);
}
}
returned value
hello
i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.
Thanks.
There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example).
The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.
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
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
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 am trying to allocate memory in a 2D array dynamically, but I don't know what is wrong.
Error
let input is
2 2
1 2 3 4 5 6 7 8 9 ........
then program crashes
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,M;
int i,j;
scanf("%d %d",&N,&N);
int **A = (int **)malloc(N*sizeof(int *));
for(i=0;i<N;i++)
{
A[i] = (int *)malloc(M*sizeof(int));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("%d",A[i][j]);
}
}
return 0;
}
Here's your problem:
scanf("%d %d",&N,&N);
When you read in the array dimensions, you read into N twice and never read into M. As a result, the contents of M are indeterminate, and attempting to read that value invokes undefined behavior.
Fix this to input a value for M:
scanf("%d %d",&N,&M);
As i said problem is in scanning N and M variables.
Change
scanf("%d %d",&N,&N);
to
scanf("%d",&N);
scanf("%d",&M);
and you are fine.
Problem was you were reading N twice while didnt read an M which was used uninitialized.
Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
You should free all your allocated memory
for(i=0;i<N;i++)
{
free(A[i]);
}
free(A);
and dont cast malloc()'s return value becouse
Its reduntand
Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found
If the type of the pointer is changed, one must fix all code lines where malloc() was called and cast
Next time if you cannot find a bug, try to use debugger and look what exactly is happening, you can see variable values, where would you clearly see that M is uninitialized and didnt change after scanf().
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++]