3 Replies - 147 Views - Last Post: 16 November 2019 - 09:34 AM Rate Topic: -----

#1 codeisnewbie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-November 19

need help with histogram using for loops

Posted 15 November 2019 - 08:40 PM

/* the code here displays the number and the corresponding asterisk
but how can i make it to the point that the numbers  display seperately
for example 123, it should display 
1*
2**
3***
*/
#include <stdio.h>

int histogram(int ans)
{
	int ctr;
	for (ctr=1; ctr<=ans; ctr++)
	{
		printf("*");
	}
	
}
int main()
{
	int num;
	printf("Enter number: ");
	scanf("%d", &num);
	histogram(num);
	return 0;
}


This post has been edited by Skydiver: 16 November 2019 - 09:45 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: need help with histogram using for loops

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2394
  • View blog
  • Posts: 4,528
  • Joined: 30-May 10

Re: need help with histogram using for loops

Posted 16 November 2019 - 03:50 AM

Keep breaking the problem down.
void printNumber(int num)
{
}
void printStars(int num)
{
}

int histogram(int ans)
{
  int ctr;
  for (ctr=1; ctr<=ans; ctr++)
  {
    printNumber(ctr);
    printStars(ctr);
    printf("\n");
  }
}


Was This Post Helpful? 0
  • +
  • -

#3 codeisnewbie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-November 19

Re: need help with histogram using for loops

Posted 16 November 2019 - 08:06 AM

/*I did break it down a little but i added a revision
if i inputed 12345 it displays
1 *
5 *****
5 *****
5 *****
5 *****
*/
#include <stdio.h>
/*
	temp = ans % 10;
	temp2 = ans / 10 % 10;
	temp3 = ans / 100;
*/
int digits(int num)
{
	if (num==0)
	return 0;
	return 1 + digits(num/10);
}
int left(int n)
{
	while (n >= 10)  
		n /= 10; 
    return n;
}

int right(int num)
{	
	while(num!=0) 
	return 10 * digits(num);
}


void histogram(int num)
{
	int ctr, ans;
	ctr=digits(num);
	while (ctr!=0)
	{
		printf("%d", left(num));
		
		for (ans=1; ans<=left(num); ans++)
			printf("*");
			printf("\n");
						
		num = num % right(ctr-1);
		ctr--;
	}
}
int main()
{
	int num;
	printf("Enter number: ");
	scanf("%d", &num);
	printf("number of digits: %d\n", digits(num));
	printf("left most side: %d\n", left(num));
	printf("getright: %d\n", right(num));
	histogram(num);
	return 0;
}
:code:

This post has been edited by modi123_1: 16 November 2019 - 09:27 AM
Reason for edit:: In the future please use the [code] tag button in the editor

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15356
  • View blog
  • Posts: 61,556
  • Joined: 12-June 08

Re: need help with histogram using for loops

Posted 16 November 2019 - 09:34 AM

I would think the nut would come down to this line. What do you believe this is doing vs what is it really doing?
39	        num = num % right(ctr-1);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1