I feel quite 'stupid' as asking this question but if anyone can show me the methods to modify the input result appeared on the command window.
Example:
I want to sort 5 numbers (1, 3, 4, 7, 5) in smallest-to-biggest order and the result on the command window must be:
input: 1 3 4 7 5 /* 1 line input */
output: 1 3 4 5 7 /* 1 line output */
Edit:
Here is my code
for (i = 0; i < 5; i++)
{
scanf("%d ", &array[i]);
}
If I use this code the result on command window must be:
1
3
4
7
5
But I want all the input number in only 1 line as:
1 3 4 7 5
So what do I have to do with my code?
Regarding to your edited question, just replace "%d " with "%d".
#include <stdio.h>
#define N 5
int main(void){
int i, j, array[N];
printf("Please enter the %d numbers.\n", N);
printf("input : ");
for(i=0;i<N;++i){
scanf("%d", &array[i]);
if(i!=0){
for(j=i;j>0 && array[j-1] > array[j];--j){
//swap array[j] and array[j-1]
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
printf("output : ");
for(i=0;i<N;++i){
if(i!=0)
putchar(' ');
printf("%d", array[i]);
}
putchar('\n');
return 0;
}
Related
This is a code for input-output practice. I am getting the correct output for the first two input lines. But I am getting a zero for the third line input.
The given task is: To calculate the sum of some integers.
Input:
4 1 2 3 4
5 1 2 3 4 5
0
Output:
10
15
#include<stdio.h>
int main()
{
int i, first, next, total;
while(scanf("%d", &first) != EOF)
{
total = 0;
for(i = 1; i <= first; i++)
{
scanf("%d", &next);
total += next;
}
printf("%d\n", total);
if(first == 0)
{
printf(" ");
}
}
return 0;
}
If you do not want output when the first number on a line is zero, then you should test first == 0 before calculating and printing a total and break from the loop (break; if you want to stop the loop) or continue to the next iteration (continue;).
I was doing some hackerrank practice and I couldnt solve it. I am newbie any ideas to solve this problem?
Please write a software program that prints the following pyramid pattern to the screen. Your program should get a number ānā as an input and should print n lines of the pyramid. (For example if your input is 4, then the following output is expected.)
(You can choose any programming language that you want.)
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
#include<stdio.h>
int main()
{
int i,j,k,l=1;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=l; k++)
{
printf("%d",k);
}
l = l+2;
printf("\n");
}
return 0;
}
These types of problems can be solved by simple mathematics.
I would advice you to solve this by problem by breaking it into simpler and smaller use cases as follows:
Number of blank spaces on each line.
Number of Elements (on each line) until the centre of pyramid.
Number of Elements (on each line) after the centre of pyramid.
#include<stdio.h>
int main()
{
int N;
scanf("%d", &N);
int numberOfBlankSpaces = N + 2;
for(int i=1; i<=N ;i++){
for(int j=0;j<numberOfBlankSpaces;j++){
printf(" ");
}
numberOfBlankSpaces -= 2;
int number_of_elements_until_centre_on_each_line = i;
int startingElement = i;
//Print increasing order until center
while(number_of_elements_until_centre_on_each_line--){
printf("%d ", startingElement);
startingElement++;
}
// Starting element = Element at center - 1
startingElement-=2;
//Print decresing order
while(startingElement >= i){
printf("%d ", startingElement);
startingElement--;
}
printf("\n");
}
return 0;
}
Input: 4
Output:
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
I'm trying to use a for loop to add a space to the beginning of my output for every line. For every line, it will add more space between as it goes through each line to get the flipped triangle.
My code
#include <stdio.h>
#include <math.h>
// compiler issue on 2nd digit
int main(){
long long number;
printf("Enter a number\n");
printf("Enter your number = ");
scanf("%lld", &number);
for(int j=1; j<= (log10(number)+1); j++){
for(int i=1; i<= (log10(number)+1) - (j-1); i++){
printf("%d ", (number%((int)pow(10, i)))/(int)pow(10, i-1));
}
printf("\n");
}
return 0;
}
My output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
What I'm trying to output:
Enter a number
Enter your number = 12345
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
A sample program as what I had in mind
int main()
{
int l;
for(l=0; l<3; l++){
printf("%*s Moving\n", l, "");
}
return 0;
}
I need to generate the following output of odd numbers in pyramid pattern.
The output will be like
1
3 3
5 5 5
7 7 7 7
I have written the following code. What portion i should modify?
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=1;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
Current Output:
Current output is like-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You could write:
for(r=1; r <= num; r+=2) //we only need odd numbers
{
times = r/2 + 1; //how many times to print odd number
for(c=1; c <= times; c++)
printf("%d",r); //print one character at a time
printf("\n");
}
You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.
you have 2 errors in this code .
m is not declared anywhere .
you are running a infinite loop
try this .
#include<stdio.h>
int main()
{
int num,r,c,m;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=r;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
In your code, instead of
m=1;
you should write
m= ( (r/2) + 1);
Oterwise, all the time, you'll be iterating in the for loop only once.
Some little modifications and it works:
#include<stdio.h>
int main()
{
int num,r,c,m=0;
printf("Enter structure number : \n");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m++;
for(c=1; c<=m; c++)
printf("%d ",r);
printf("\n");
}
}
return 0;
}
m is undeclared
line feed at the end of the printf message
m incremented each odd iteration
space between printed unmbers
Here is a demo.
the inner for loop should look like this:
for(c=1; c <= r/2; c++)
printf("%d ",r);
just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?
like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.
you can run the code here on ideone.com
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
int m=r;
for(c=1; c<=m; c++)
{
if(c%2 != 0){
printf("%d ",r);
}
}
printf("\n");
}
}
return 0;
}
and test
sh-4.3# main
Enter structure number : 9
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.
You can check this:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",2*r-1);
printf("\n");
}
return 0;
}
Please take a look at this:
for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
for(c=1; c<=r; c+=2)// increment by 2
printf("%d",r);
printf("\n");
}
If you want alternate numbers like 1,3,5,7... just increment value by 2.
Im working on an optional test review problem for an introduction to C class, and I need to have a program that prints out the following based upon what ever number a user enters:
Enter a number: 5
5
44
333
2222
11111
000000
11111
2222
333
44
5
So far this is the code that I have written
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",space);
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 1;space <= row;space++){
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}
This is the output that I get
Enter a number: 5
0
11
222
3333
44444
555555
22222
3333
444
55
6
I've spent quite a few hours trying to figure out how to print the upper half of the half diamond using the user entered numbers but I can't seem to figure it out. Could anyone point me in the right direction?
Your numbers are just off a bit, correct the printf calls and you're done:
First one:
printf("%d", number - space);
Second one:
printf("%d", space - 1);
A slightly better (more readable and a bit more logical) way would be to use other variables instead:
First one:
printf("%d", number + 1 - row);
Second one:
printf("%d", row);
Also note that some basic math can help you to realize the following:
Total number of rows: 2 * number + 1
Number of spaces: 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 => abs(number - row) (If starting your rows with 0)
Number to print: Same as "Number of spaces"
Number count: 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 => 6 - number_of_spaces
This gives a much cleaner, more readable version with only one loop:
#include <stdio.h>
#include <math.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 0; row <= number * 2; row++){
int number_of_spaces = abs(number - row);
int number_to_print = number_of_spaces;
int number_count = 6 - number_of_spaces;
for (space = 1; space <= number_of_spaces; space++){
printf(" ");
}
for(column = 1;column <= number_count; column++){
printf("%d", number_to_print);
}
printf("\n");
}
}
return 0;
}
To expand on schnaader's answer (which is perfectly fine and complete), you can improve your code even more, letting printf() do the spacing for you rather than doing a loop and several calls to printf():
printf("%*s", width, "");
Here width is replaced with the calculated space you'd like to fill. The precision * is a special placeholder that tells the function to take the actual precision/length from the parameter list. Since the string to print is empty, it will always fill the whole range with space characters.
for (row = 0; row <= number + number; row++) {
int t = row * (row <= number) + (number + number - row) * (row > number);
printf("%*c", number - t, ' ');
printf("%*d\n", t + 1, t);
}
There are numerous ways, I've just modified your code
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",number -space); //change1
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 0;space < row;space++){ //change 2
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}
the following code outputs the first half of the problem.
Notice the checking for errors in the call to scanf()
It compiles cleanly and works correctly
I leave it to you to complete the function for the second half of the output
which should be easy now that you have the first half
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row;
int i; // loop counter/index
int n; // user input
printf( "\nEnter the wedge width: ");
if(1 != scanf(" %d", &n) )
{ // then, scanf failed
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
for( row = 0; row <= n; row++ )
{
// print n-row ' '
for( i=0; i<(n-row); i++)
{
printf(" ");
}
// print row+1 '(n-row)'
for( i=0; i<(row+1); i++)
{
printf( "%1d", (n-row));
}
printf( "\n" );
}
// reverse the calculations to print the lower half
return 0;
} // end function: main