failing to display array entered from keyboard in c - c

I'm new to c and I'm trying to write a c program that get 10 integer values entered from keyboard using scanf and then print them using printf but the result is not correct. Here is the code:
#include<stdio.h>
#include<conio.h>
main(){
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
for(int i=1;i<=10;i++){
printf("\n\tEnter Score %d", i);
scanf("%d",x);
}
printf("\n\t The entered scores are: %d",x[i]);
return(0);
}
the output given is a four digit number like 8731 yet I expect something like 1234567890. some help please

You need to make a new for loop to display the values, just like you do when reading them.
PS: format your code better, you'll thank it later.
PS2: try to avoid conio.h, it's not standard, and you don't even need it for your code.
PS3: also your code is wrong. Should be for(int i=0;i<10;i++). Arrays go from 0 to size-1, not from 1 to size. The C compiler will not warn you that i[10] is an invalid index for your array.

Try this:
#include<stdio.h>
#include<conio.h>
int main(){
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
for(int i=0;i<10;i++){ //Change 1
printf("\n\tEnter Score %d", i);
scanf("%d",&x[i]); //Change 2
}
//Change 3
for (int i=0; i<10; i++)
printf("\n\t The entered scores are: %d",x[i]);
return(0);
}

#include<stdio.h>
#include<conio.h>
The header <conio.h> is not standard. You get better portability if you don't use it. Anyway, your program doesn't use anything from it.
main(){
The function main() returns an int. Get into the habit of saying so explicitly (and you might also get into the habit of objectively specifying it takes no parameters).
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
For better working of printf() in all implementation, end each one with a '\n'. Otherwise the output might appear out of order.
for(int i=1;i<=10;i++){
printf("\n\tEnter Score %d", i);
scanf("%d",x);
Trying to always read to the same position in the array in a loop?
}
printf("\n\t The entered scores are: %d",x[i]);
The element x[i] does not exist. At this point in the code, i is larger than the largest legal array index.
return(0);
}

Related

Getting incorrect output for a basic multiplication in my C programme

I'm new to learning C, and practicing some basic code.
I keep getting either a 0 or a very large random number for the bill multiplication at the end. I think I've narrowed it down to an issue with my "quant" value but I can't figure it out. Here's the first part of the programme (that only calculates for the pizza options so far):
#include <stdio.h>
int main()
{
int num, cho, quant, bill;
printf("Select a number from the menu:\n1. Pizza\n2. Burger\n3. Sandwiches\n4.
Beverages \n");
scanf("%d",&num);
if(num==1)
{
printf("Select a number for the food you want to order, and the quantity. \n");
printf("1. Pepperoni\t\tRs 1000 \n2. Fajita\t\tRs 1000\n3. Hot n spicy\t\tRs 1100\n4.
Chicken Tikka\tRs 1200\n");
scanf("%d,%d",&cho,&quant);
{
if(cho==1)
cho=1000;
else if(cho==2)
cho=1000;
else if(cho==3)
cho=1100;
else if(cho==4)
cho=1200;
}
bill=cho*quant;
printf("Please take your food item(s). Total bill = Rs %d \nThank you.",bill);
}
getchar();
getchar();
getchar(); getchar(); getchar();
return 0;
}
The way you have your scanf for cho and quant right now expects the user to enter two numbers separated by nothing but a comma, i.e. 2,10. If you do that format of input, your code works fine. However, if this format is broken the scanf does not properly read in a number to one or both of your variables, and they are left unitialized with potentially garbage information in them (hence the really large random numbers you're seeing).
You should probably change that line to
scanf("%d%d",&cho,&quant)

VS Code is showing weird numbers before the output? How to fix this?

When running my code in VS Code, it is showing a random weird number in the output:
#include <stdio.h>
void main()
{
int a;
printf("Enter a no: ");
scanf("%d", &a);
if (a%2==0) {
printf("%d It is even no");
}
else{
printf("%d It is odd no");
}
}
As Gino's said, the problem is that you have a %d at the beginning of each of your printf() calls. This tells printf() to pull a number from its argument list. Since you didn't provide a number in the argument list, it pulls the next number on the stack, whatever that happens to be. It's probably a local variable.

Infinite for loop? ()in c

the value of i resets after it reachers 7
#include <stdio.h>
int main(){
char marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
output
Problem is here:
scanf("%d", &marks[i]);
Specifier "%d" expects a pointer to int, not char. Usually it will write 4 bytes what is a typical size of int.
Therefore on 8th iteration the elements of marks at index from 7 to 10 are touched. However, marks[10] is outside of marks array (only indices 0-9) are valid. Undefined Behaviour is invoked and the program can do anything, from crashing to infinite looping or conjuring nasal deamons.
To fix the program change the type of marks to int:
int marks[10];
Note:
UB is invoked even on the first iteration because "%d" expects a pointer to int while type of &marks[0] is char*. This operation is undefined by C standard because int* and char* may differ in size and/or representation and/or alignment. However it is a unlikely case for modern CPUs.
You have declared the marks as a character array and tried to get input from user using %d which asks asks for an integer,
#include <stdio.h>
int main(){
int marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
Also I didn't understand the use of all so I couldn't find a solution for it. If you want to print a specific number then you have to specify it like you have done it for second or if you want to display the total you want to add a furthermore code to calculate the sum of elements in the array.

How do I determine if a value is within a range?

I am relatively new to C, I have to do it for school unfortunately and I am having issues with it at the easiest exercises.
Here I have to check if a number is in a certain interval, for example between 4 and 6. I made it like this.
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%s", i);
if (i>4 && i<6){
printf("%s Value is in first interval\n", i);
}
}
The scanf to enter the number and check if it is in the interval. But even if I enter a number that is part of it, for example 5, the printf doesn't do anything. I tried also to add an else statement for numbers outside the interval, but also there the printf did not change anything.
It is because you have declared i variable as int and you are taking input as string so when it is checking condition it is getting null value in i variable and not able to enter if block check below code
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%d",&i);
if (i>4 && i<6){
printf("%d Value is in first interval\n", i);
}
}
try compiling your code without if condition i variable will return a null value

Find the sum of two numbers which are in the separate line?

So there is a problem on SPOJ as mentioned below:
Given two natural numbers (both not greater than 200), each number in the separate line, please print the sum of them.
Example Input:
2
3
Output: 5
So I wrote a program to this problem. Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int input1, input2, sum;
printf("Enter two natural numbers\n");
scanf("%d", &input1);
scanf("\n%d", &input2);
if ((input1>0&&input1<=200) && (input2>0&&input2<=200))
{
sum = input1 + input2;
printf("%d", sum);
}
return 0;
}
But SPOJ rejected this answer as a wrong answer.
Later I checked this on idone.
But I'm unable to detect, what's wrong with this answer, as expected it gives the same output given in above question.
Please correct if I'm wrong.
The first print statement
printf("enter two natural numbers\n");
The Second
scanf("%d",&input1);//Press Enter
The Third
scanf("%d",&input2);//Press Enter
And finally
printf("\n%d",sum);
First of all, remove the printf statement as it is not needed and will mess up the expected I/O as given by SPOJ.
Next, there is no need of the newline character in scanf. You can directly write scanf("%d %d", &input1, &input2);. Another way would be to write the scanf statement twice as:
scanf("%d", &input1);
scanf("%d", &input2);
Lastly, you can also remove the if statement, if input bounds are given by SPOJ.

Resources