Two Homework assignments need for loops, yet...

Hello there!

I have two homework assignments that require me to use for loops for algorithms, I'm assuming.

For the first assignment, I have to make an hourglass using asterisks. There are two things the user has to input, how many "#'s" are on the top line and how big the hourglass is. For example:

Enter size of the top row: 5
Enter number of rows: 3

#####
###
#####


That's the solution, but I don't know how to do it using for loops... here's my code at this point:

http://cpp.sh/8wxc3



The for loops at the end are not from me, really.
How would I go about doing it? I don't understand where I should I go.... That's this assignment.


The second one is similar in code, I'm guessing. But I have to have it print like:

enter min range= 3
enter max range= 5

Factors of all numbers in the range: [ 3,5 ]
5: 1, 5
4: 1, 2, 4
3: 1, 3


Here's my code for this one at the moment:

http://cpp.sh/8xgv



Any and all help that would point me in the right direction would be much appreciated! <3
Last edited on
you should use two for loops for hour glass printing..
based on printed output you will adjust the desired output with required spaces..
I know that, but I don't know how to do it....i've been stuck at this point for over 6 hours...
Try this looping,
hope it helps i adjusted so far for getting desired output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(i=1;i<=num;i++)
        {
                for(j=1;j<i;j++)
                        cout<<" ";
                for(j=0;j<=num-i;j++)
                        cout<<"# ";
          cout<<endl;
        }

        for(i=1;i<num;i++)
        {
                for(j=1;j<num-i;j++)
                        cout<<" ";

                for(j=0;j<=i;j++)
                        cout<<"# ";

         cout<<endl;
        }

It worked when I changed num to userSize, because that's what I had it set to. But it's a little off of what I need.
I'm sorry for how long this pastebin is, but it's the directions of my homework.

http://pastebin.com/tRBaNmNj


The problem with this code is the "#'s" are spaced apart, and there are too many lines leading up to the middle.... It's hard to explain, sorry, but here's what it should look like using 5 as the most #'s and 2 being how many lines until the middle:

http://imgur.com/DFCc1s9



Could I ask what your reasoning behind that code was? I'm trying to think of how to handle it, but I can't think of it mathematically. What code interacts with what? What does each for statement mean? My teacher didn't really cover this well, so it's confusing me, a lot.


edit: I changed the num to rows, and I got more what I was looking for. However, the spacing problem was still there, and there weren't enough showing up. For example: doing 5 and 2 made it only having 2 on top and bottom, not what I needed.

Thank you for your time.

Last edited on
I will not do your assignment for you, however I will give you a head start. Here is some code I wrote below that should help you understand more of what the for loop is suppose to do here. Your for loop that was included in your .cpp file was not working for me. You may have to go through my code and edit some of the syntax based on what your professor wants. This is just an outline. Hope I helped,

Zachary Law

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
  File: hour_glass
  Created by: Dallas Webb
  Creation Date: 10/06/2015
  Synopsis: 
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  // Declare and initialize variables
int userSize;
int rows;

  // Repeatedly prompt for top row size until valid value is entered
	cout << "Enter size of the top row: ";
	cin >> userSize;

	while (userSize <=2)
	   {	
		cout << "Size of the top row must be at least three." << endl;
		cout << "Enter size of the top row: ";
 		cin >> userSize; 
	   }

	
 // Repeatedly prompt for the number of rows until valid value is entered

	cout << "Enter number of rows (3): ";
	cin >> rows;
	
	while (rows != 3)
	{
		cout << "Invalid number of rows, please enter 3";
		cin >> rows;

	}

// Print the hour glass

	if (rows == 3)
	{
	
			for(int i = 0; i < userSize; i++)
				{
					cout << '#';
				}
	
	cout << endl;

			for (int i = 0; i < rows; i++)
				{
					cout << '#';
				}

	cout << endl;

				for(int i = 0; i < userSize; i++)
				{
					cout << '#';
				}
			
      }
  // end program

	system("pause");
  return 0;
}
Last edited on
Topic archived. No new replies allowed.