A simple question from a C beginner, about arrays [duplicate] - c

This question already has answers here:
read int with scanf until new line
(3 answers)
How to get integer input in an array using scanf in C?
(4 answers)
Closed 2 years ago.
I'm new to C language and I'm trying to make a program that includes an array of type int with the max length of 10.
I want to enter a random number of 1-digit numbers and set all the other values to 0.
For example, the console will look like this:
Enter B Values : 1 2 5 7 8
and the B array will look like this 1 2 5 7 8 0 0 0 0 0
I'm trying to add the numbers at the same line without using Enter.
Could anyone help?
Could the answer be EOF related?

Related

0 at the begining of value isnt shown in the output [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 2 years ago.
my question is short, if I have the following 2 lines of code:
int var = 01;
printf("%d", var);
the output is : 1
how do I get 01 rather than 1?
Use left padded format string.
Solution
int var = 1;
printf("%02d", var);

how to control 2d character array in c? [duplicate]

This question already has answers here:
C scanf() and fgets() problem
(4 answers)
Closed 3 years ago.
This is a problem of URI online judge.Problem no.1914 beginner.According to my code if i give input 4 then the the program should read strings 4 times and also integer 4 times each time two integer.but the program are only taking 4 times input either 4 times string or 4 times integer or 2 times string+ 2 times integer.
#include <stdio.h>
int main()
{
char name[1000][100],ch;
int a,b,c,i,j=0,k,n[1000][2];
scanf("%d",&a);
for(i=0;i<a;i++)
{
gets(name[i]);
for(k=0;k<2;k++)
{
scanf("%d",&n[i][k]);
}
}
}
if inputs are
4
Quico PAR Chiquinha IMPAR
9 7
Dami PAR Marcus IMPAR
12 3
Dayran PAR Conrado IMPAR
3 1000000000
Popis PAR Chaves IMPAR
2 7
After taking 4 lines of input the program end.If u can help help with that please help.
quico PAR chiquinha IMPER
9 7
dami PAR marcus IMPER
12 3
Read this- https://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/
Your scanf is leaving a newline on the buffer.

force length of int from a sensor [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 6 years ago.
I get data from a sensor in int type but the data range from 0 to 999.
I need a simple way for the var to always be the same length.
Like if I get 123 I don't need to change it but if it's 43 I need it to be 043.
Is there a simple way to do that?
Thanks.
Try with:
printf("%03d", var);

How to randomize numbers with no repeat in C? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

unexpected result from printf character print [duplicate]

This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 years ago.
#include<stdio.h>
main()
{
printf("%c\n",1-3+2["nexus"]);
}
The result is v. How does it turn out?
What does the indentation (square ones) do?
2["nexus"] is probably leading us to 3 element of the nexus considering it as an
array of characters[arr[2]=x in our case]
following is 1-3viz -2 added to x character ie added to to ascii value
and the corresponding element is v
cant exactly figure out what the square identation really mean but according to the oput this is possible way .
2["nexus"] is same as "nexus"[2], which correspond to the 2nd index or the 3rd element of this string. ( Which is 'x' here ).
In equation, 1-3+ 2["nexux"] is like 1-3+'x' that represent 'v'.
Note
When a string is declared like "nexus".
nexus[0] is 'n'
nexus[1] is 'e'
nexus[2] is 'x'
nexus[3] is 'u'
nexus[4] is 's'

Resources