The question to solve is that this code should get numbers in separate lines until 0 is given. Then it should print y number, y times. For example if number 3 is given, it should print 3, 3 times in separate lines.
I try to get the inputs from the user in separate lines. I mean one input in one line. Then print the numbers in separate lines. I don't know where to add \n
to solve it.
This is my code:
#include <stdio.h>
int main() {
int y = 1;
while (y != 0) {
scanf("%d", &y);
if (y == (0)) {
break;
}
for (int i = 1; i <= y; i ) {
printf("%d\n", y);
}
}
}
I tried to add \n
beside %d
of scanf
but it didn't work as I expected.
The output of this code is like this:
1
1
2
2
2
3
3
3
3
4
4
4
4
4
0
What I expect is that all the inputs should be given in separate lines before output is printed. input like this:
1
2
3
4
0
output like this:
1
2
2
3
3
3
4
4
4
4
CodePudding user response:
As @DavidC.Rankin explains below, if you want to retain the input it means you have to store it somewhere. It's usually in memory and either the original input string (char []
) or you store the data in a format suitable for output like int input[LEN]
that I use below.
Other options includes using a recursive function to store one number on the stack, or a file or an external service like a database (possible hosted in the cloud these days).
#include <stdio.h>
#define LEN 5
int main(void) {
// input
int input[LEN];
int i = 0;
for(; i < LEN; i ) {
if(scanf("%d",input i) != 1) {
printf("scanf failed\n");
return 1;
}
if(!input[i])
break;
}
// output (copy of input other than 0)
for(int j = 0; j < i; j ) {
printf("%d\n", input[j]);
}
// output (repeated based on input)
for(int j = 0; j < i; j ) {
for(int k = 0; k < input[j]; k ) {
printf("%d\n", input[j]);
}
}
}
and example run:
1 # input
2
3
4
0
1 # output (copy of input other than 0)
2
3
4
1 # output (repeated based on input)
2
2
3
3
3
4
4
4
4
Here is the as close you can get with a recursive function and no arrays:
#include <stdio.h>
#include <stdlib.h>
void read_then_print() {
// input
int d;
if(scanf("%d", &d) != 1) {
printf("scanf failed\n");
exit(1);
}
if(d) {
// output before recursion
printf("%d\n", d);
read_then_print();
}
// output after recursion
for(int i = 0; i < d; i ) {
printf("%d\n", d);
}
}
int main(void) {
read_then_print();
}
and example session:
1 # input
1 # output before recursion
2 # input
2 # output before recursion
3 # ...
3
4
4
0
4 # output after recursion
4
4
4
3
3
3
2
2
1
CodePudding user response:
#include<stdio.h>
int main()
{
int y;
do
{
scanf("%d",&y);
for(int i = 1;i <= y;i )
{
printf("%d\n", y);
}
}while(y != 0);
}
Use this one this might solve your problem.
CodePudding user response:
You want to gather all the input before processing the lines and printing the results. You can use an array to store the input numbers and process them once you read a 0
.
Here is a modified version with this approach:
#include <stdio.h>
#define MAX_LINES 256
int main() {
int input[MAX_LINES];
int y, n = 0;
while (n < MAX_LINES && scanf("%d", &y) == 1 && y != 0) {
input[n ] = y;
}
for (int i = 0; i < n; i ) {
y = input[i];
for (int i = 0; i < y; i ) {
printf("%d\n", y);
}
}
return 0;
}