3 Replies - 12480 Views - Last Post: 09 October 2008 - 11:13 AM Rate Topic: -----

#1 shangyi  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 15-June 08

help with nested loop to display patterns using C

Post icon  Posted 09 October 2008 - 05:16 AM

hello guys, so i am asked to write a programme that produces the following pattern by suing nested loop and a if else statment to do that:

* 2 3 4 5 6 7 8 9
1 * 3 4 5 6 7 8 9
1 2 * 4 5 6 7 8 9
1 2 3 * 5 6 7 8 9
1 2 3 4 * 6 7 8 9
1 2 3 4 5 * 7 8 9

i have alot of problems to do something simple like this. Here is my code:

#include <stdio.h>

int main()
{
	int number;
	int star =1, star_location = 1;
	int numberzcounter;

for(numberzcounter=1;numberzcounter<7;numberzcounter++)
{ 
	for(number = 1;number<10;number++)
	{
		if(star == star_location)
		{
		star_location++;
		printf("* ");
		}
		else
		{
		printf("%d ", number);
		star++;
		}
	}
		
		printf("\n");
}

return 0;
}



and it produced this output which is obviously wrong.

* 2 * 4 * 6 * 8 *
1 * 3 * 5 * 7 * 9
* 2 * 4 * 6 * 8 *
1 * 3 * 5 * 7 * 9
* 2 * 4 * 6 * 8 *
1 * 3 * 5 * 7 * 9

can anyone help me please? i am still unable to produce the desired pattern.

Thanks in advance.

This post has been edited by shangyi: 09 October 2008 - 05:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help with nested loop to display patterns using C

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: help with nested loop to display patterns using C

Posted 09 October 2008 - 08:57 AM

This is C++ so you'll have to convert it. However, it produces the required output as instructed.

int x, y;
int star=1;//start out star variable equal to the number you want replaced first
for(y=1; y<=9;y++)//do this 9 times so you'll have 9 rows
{
    for(x=1;x<=9;x++)
    {
    
    if(star==x) //tell it which number to replace each time
    {
    cout<<"*";
    }
    else //otherwise cout the rest of the numbers
    {
    cout<<x;
    } 
}
star++;//increment your counter here so star will ==the next number for the next iteration of the loop
cout<<endl;
} 



I hope this helps you!
:^:
Was This Post Helpful? 0
  • +
  • -

#3 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: help with nested loop to display patterns using C

Posted 09 October 2008 - 10:24 AM

View PostOliveOyl3471, on 9 Oct, 2008 - 08:57 AM, said:

This is C++ so you'll have to convert it.

Lol. Just change all cout<<"lalalala"<<integerVar to printf("lalalalaa %d",integerVar);.

Olive, you're that lazy? :lol:
Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: help with nested loop to display patterns using C

Posted 09 October 2008 - 11:13 AM

lol you caught me. ;)
Actually I was in class this morning, and I wrote that code during the break (between classes) so it was quick.

This post has been edited by OliveOyl3471: 09 October 2008 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1