My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
February 21st, 2013, 03:29 AM   #1
Senior Member
 
Joined: Apr 2010

Posts: 128
Thanks: 0

C program , looping.

Write a C program using pseudocode below :
 
1. Start
2. Ask user to enter a number from 1 to 9 or enter 0 to quit.
3. If user enter 0, goto line
4. If user enter number from 1 to 9
5. Display a box with row and columns using the number
entered by user.
(example if user enter 4, display box as below :
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
6. Repeat line 2
7. End

My work :
#include <stdio.h>

main()
{
int integer;
printf("Enter 1-9 to proceed , enter 0 to quit\n");
scanf("%d",&integer);

if(integer<1)
{
return 0;
}

while(integer>9)
{
printf("Enter 1-9 to proceed , enter 0 to quit\n");
scanf("%d",&integer);
}


for(integer;integer>0;integer--)
{
{printf("%d",integer);}
}

return 0;
}

I can't even obtain one line of equal number....how to do it?
rnck is offline  
 
February 24th, 2013, 06:52 AM   #2
Member
 
chocolatesheep's Avatar
 
Joined: Jul 2012

Posts: 60
Thanks: 0

Math Focus: Calculus
Re: C program , looping.

I'd imagine your program just prints out one line of numbers decresing in size
Code:
for(integer;integer>0;integer--)
 printf("%d",integer);
All this does is it takes the number you enter, prints it to the screen, decreases it by 1, then prints it out again.

You do not want to decrease the number at all. You want to print the same number in every loop.
You need 2 for loops, and a counter.

Code:
for(int i=0; i<integer; i++)
{
  for(int j=0; j<integer; j++)
       printf("%d",integer);
   
  printf("\n");
}
The printf("\n"); is to enter a new line (I think you can do it like that in C not sure though lol)
I have 2 counters, i and j.
When i=0 the first foor loop begins. It gets to the second for loop and j=0. It prints your number (for example 6) once, then j=1, then it prints 6 again, then j=3 then it prints 6 again, and so on, until j=6. When j=6 the second for loop will stop there because j is no longer < than your number 6. So by the time j=0, it would have printed your integer 6 times to the screen. It would look like this
6 6 6 6 6 6
After that it enters a new line. This whole time we were inside the first for loop and i is still 0. Now the first for loop has completed one cycle. Now i=1 and we start all over again. The second for loop starts again and we end up with another 6 character line of the number 6. After this we edn up with
6 6 6 6 6 6
6 6 6 6 6 6
The program again enters another line and for loop no. 1 starts all over again, now for i=2
Everything keeps repeating like this until i=6, because 6 is not < 6
By the end, you will have 6 character long and 6 character high square made up of the number 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
It works with any other number too.
The code before your for loop is ok, just add the code I wrote abve
chocolatesheep is offline  
February 25th, 2013, 04:23 AM   #3
Math Team
 
Joined: Apr 2010

Posts: 2,780
Thanks: 361

Re: C program , looping.

Or how about this?

Code:
for(int i=0; i<integer; i++)
      printf("%d",integer * ((10^integer-1)/9));
Hoempa is offline  
February 27th, 2013, 08:01 AM   #4
Senior Member
 
Joined: Apr 2010

Posts: 128
Thanks: 0

Re: C program , looping.

Done!

the X and Y variable had me struggled.....

because i can only do one input.

in the end i put x=integer , x<10 , x--
rnck is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
looping, program



Search tags for this page
Click on a term to search for related topics.
Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matlab AND PDE (Program) Ayour Applied Math 0 February 28th, 2014 01:43 AM
Error in Program - cpp sdj Computer Science 1 December 13th, 2012 05:22 AM
What do you guys think of this program..? rage Academic Guidance 2 July 5th, 2012 07:51 AM
computer program Hoempa Computer Science 0 June 21st, 2010 05:37 AM
Factorial code WITHOUT looping/recursion Balawton Computer Science 10 January 22nd, 2010 09:38 PM





Copyright © 2019 My Math Forum. All rights reserved.