If Statement doesn't even seem to exist [closed] - arrays

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Im trying to write a program that takes array inputs from the user and assigns the the odd elements to the first index until there are no more odd elements and the even elements are assigned to the end of the array until they're done so for example
Assuming this a size 10 array,The user enteres 1 for the first element and 2 for the second element and 3 for the third element ,so the final array would have indices 0 and 1 to have the values of 1 and 3 and indice 10 to have the value of 2 and so on and so forth,and here's my code
int main() {
int array1[31];
int array2[31];
for(int i=0;i<31;i++) {
if(scanf("%d",&array1[i])%2==0) {
array2[31-i]=array1[i];
}
else {
array2[i]=array1[i]
}
}
for (int i=0;i<31;i++) {
printf ("%d\t",array2[i]);
}
return 0;
}
But this code is only printing exactly what the user has entered in the exact same order,it's like my if condition doesn't execute and im not sure why,I'm still a beginner in C so I apologize if this problem is too trivial,but yeah any help is appreciated

First of all, learn that scanf() does not return the matched value, it returns the number of items matched. You should
Check the scanf() return value against the number of expected matches (1 in this case)
If scanning is success, use the supplied argument to check the scanned value.
If scanning is failure, clean up the input buffer and ask for input again.
That said, in the code, when you start the loop from i value of 0, you cannot use
array2[31-i]=array1[i];
as, for i value of 0, it will be like
array2[31]=array1[i];
which is off-by-one, you need to use
array2[31-i-1]=array1[i];
So that your array indexes are [0,31).

Related

Two same type of C codes giving different output . Need help understanding this [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 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.

Cannot change number in array into one I want [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
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).

Segmentaion fault in c while displaying array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I have to take 'n' lines of character array(size m), which will have numbers only, and i have to put the numbers in a 2-d integer array.
I am getting segmentation fault while displaying the integer array for second time.
int t,m,n,i,j,pix[182][182];
char ch,pixel[183];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m); //take n and m
for(i=0;i<n;i++){
printf("\n");
scanf("%s",pixel); //take character array
for(j=0;j<m;j++){
pix[i][j]=pixel[j]-48; //put numbers in integer array
dis[i][j]=0;
printf("%d ",pix[i][j]); //no error here
}
}
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<m;j++)
printf("%d",pix[i][j]); //segmentation fault after n-1 lines are displayed
What is the problem?
Your code makes no effort to check for array boundaries. You need to check that m and n are less than 182 before proceeding. You should also use a method that protects against a buffer overrun when reading the pixel array - e.g fgets(pixel, sizeof(pixel), stdin). Otherwise, depending on the values on m and n you could have a buffer overrun with unpredictable results.
Your example also did not show the definition of the dis array, but you need to do a boundary check there as well.
Aside from that, I am guessing that your input is not laid out quite like your program expects. Performing array boundary checks and asserting as soon as they fail will help you find the discrepancy much quicker.

Using Do While in C [Find Mistake] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I have a relatively simple program that asks for user input between a range and then checks it using Do - while loop.
int n;
do
{
print ("hello, world\n");
n = getvalue() //just making this syntax up.It takes value from user
}
while (n<0 && n>99);
print ("%d\n",n);
I want to prompt the user to input another value if he/she enters either a negative number or a triple digit number (or higher). But the condition within while is not being validated.
What the output looks like:
No matter what number I enter, it get printed. i.e. -2 is printed as -2, 101 as 101 and 50 as 50. Ideally, -2 should prompt user to enter a number again, so should 101 and only 50 should print out.
I think the problem is your rather cryptic condition of n < 0 && n > 99, which can't reasonably be satisfied.
You need to look more closely at the logic of your loop. If you want to prompt the user again if they enter a number less than zero or greater than 99, you need to use the logical or operator ||.
while (n < 0 || n > 99);

C Assignment Arrays Error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
The question is: Declare an array of type 'char' called "letters" with a size of 2. Assign the variable "alpha" as the first element of the array "letters".
I tried both:
char letters[1];
letters = alpha;
and
char letters[] = {alpha};
The program keeps saying that both of these answers are wrong. What am I doing wrong? Or is an error with the check system?
There are several mistakes in your answer:
You missed the "of size 2" part,
The second assignment has to go the other way,
The first element of the array is at index zero.
You need to add initialization to avoid undefined behavior:
char letters[2] = {'x', 'y'};
char alpha = letters[0];
in the first example you are not defining an array of size 2 but of size 1, and (this is the actual error), you're assigning a variable of type char to an array and not to a position of the array...
a simple way:
char letters[2]; // array of size 2
char alpha = letters[0]; // assigning the first position of letters array to the alpha variable
Well, using a 1 instead of a 2 should have tipped you off. And if you want to assign to an element of letters, you probably need to say something more than just the name.

Resources