Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Wondering if someone could help me real quick. C++ program
New Posts  All Forums:Forum Nav:

Wondering if someone could help me real quick. C++ program

post #1 of 4
Thread Starter 
Attempting to write a program, and I did it fairly strange but I still feel as if I can make it work.


The user input represents the total number of lines in the pyramid. You must use nested loops to implement your program.
(Hint: Each line has three parts: No of leading spaces, descending integers, and ascending integers. Consider line 3 in the above example: there is one leading space, the descending integers are 3, 2, 1 and the ascending integers are 2, 3. Try to find formula to compute the no. of leading space, ascending integers, and descending integers with to total number of lines.
)
Warning: Spoiler! (Click to show)

#include

using namespace std;
int main()

{
int First, Second, Third, Initial, Final=15, Space=0;

cout <<" Please enter an Integer from 1 to 15" << endl;

cin >> Initial;

Final=Final-Initial;

Second=Initial;

for (Third=15;Third<=15 && Third>0; Third--)
{
cout << " "; //each value of final results in a space
}
for (Initial; Initial <=15 && Initial >0; Initial--)
{
cout << Initial << " ";
}
for (Initial=2; Initial <= Second && Initial >0; Initial++)
{
cout << Initial << " ";
}
}
Trying to figure out how I can do it.
I figure I have to have a loop that with add one until it equals the initial input, and that would be the way to setup the rows.
But I'm not sure how to break the rows up in a backwards fashion from what I have started with...

I did use unneeded } but I feel it separates the code and I can remove them later.


IF you know of a better way to do it let me know, I know there are easier ways but I just started here.
Main Computer
(13 items)
 
  
CPUMotherboardHard DriveHard Drive
i7 - 4770k Asrock Z87 Extreme 4 Crucial MX300 525GB SSD Western Digital 5 Terabyte Hard Drive 
CoolingOSMonitorMonitor
Corsair H100i Windows 10 64-bit (Pro) 27" Qnix 1440p DVI 27" Qnix 1440p Multi-Input 
KeyboardPowerCaseMouse
Logitech G710 Corsair AX760 Phanteks Enthoo Pro Mionix 7000 
Mouse Pad
Mionix 
  hide details  
Reply
Main Computer
(13 items)
 
  
CPUMotherboardHard DriveHard Drive
i7 - 4770k Asrock Z87 Extreme 4 Crucial MX300 525GB SSD Western Digital 5 Terabyte Hard Drive 
CoolingOSMonitorMonitor
Corsair H100i Windows 10 64-bit (Pro) 27" Qnix 1440p DVI 27" Qnix 1440p Multi-Input 
KeyboardPowerCaseMouse
Logitech G710 Corsair AX760 Phanteks Enthoo Pro Mionix 7000 
Mouse Pad
Mionix 
  hide details  
Reply
post #2 of 4
Thread Starter 
Well, kind of small update.
I was able to get the "pyrmaid" shape to the program, but instead of starting at 1 and going down to the number I choose below 15, it goes from the number I choose and goes down to 15.... Warning: Spoiler! (Click to show)

#include

using namespace std;
// integers from 1 to 15
int main()
{
int First, Second, Third, Initial, Final=15, Space=0;

cout <<" Please enter an Integer from 1 to 15" << endl;

cin >> Initial;

Final=Final-Initial;

Final=Final+1;
while (Initial <= 15 && Initial > 0)
{
Final=Final-1;
Second=Initial;
for (Third=Final;Third<=15 && Third>0; Third--)
{
cout << " ";
}
for (Initial; Initial <=15 && Initial >0; Initial--)
{
cout << Initial << " ";
}
for (Initial=2; Initial <= Second && Initial >0; Initial++)
{
cout << Initial << " ";
}
cout << endl;
}
Initial--;
}
Main Computer
(13 items)
 
  
CPUMotherboardHard DriveHard Drive
i7 - 4770k Asrock Z87 Extreme 4 Crucial MX300 525GB SSD Western Digital 5 Terabyte Hard Drive 
CoolingOSMonitorMonitor
Corsair H100i Windows 10 64-bit (Pro) 27" Qnix 1440p DVI 27" Qnix 1440p Multi-Input 
KeyboardPowerCaseMouse
Logitech G710 Corsair AX760 Phanteks Enthoo Pro Mionix 7000 
Mouse Pad
Mionix 
  hide details  
Reply
Main Computer
(13 items)
 
  
CPUMotherboardHard DriveHard Drive
i7 - 4770k Asrock Z87 Extreme 4 Crucial MX300 525GB SSD Western Digital 5 Terabyte Hard Drive 
CoolingOSMonitorMonitor
Corsair H100i Windows 10 64-bit (Pro) 27" Qnix 1440p DVI 27" Qnix 1440p Multi-Input 
KeyboardPowerCaseMouse
Logitech G710 Corsair AX760 Phanteks Enthoo Pro Mionix 7000 
Mouse Pad
Mionix 
  hide details  
Reply
post #3 of 4
I added some comments to your latest code.
Warning: Spoiler! (Click to show)
Code:
#include <iostream>

using namespace std;
// integers from 1 to 15
int main()
{
    int First, Second, Third, Initial, Final=15, Space=0;
    // Give more meaningful names to your variables

    cout <<" Please enter an Integer from 1 to 15" << endl;

    cin >> Initial;
    // Check for errors after input

    Final=Final-Initial;

    Final=Final+1; // Instead of +1 here and -1 in the first line inside the
                   // while loop, just move Final=Final-1 to the end of the loop

    // You're looping from <input> to 15
    // What you want is to loop from 1 to <input>
    while (Initial <= 15 && Initial > 0)
    {
        Final=Final-1;
        Second=Initial;
        for (Third=Final;Third<=15 && Third>0; Third--)
        {
            cout << " ";
        }

        // Don't modify loop condition variables inside the loop!!
        // Copy "Initial" to "Num" or something like that
        for (Initial; Initial <=15 && Initial >0; Initial--)
        {
            cout << Initial << " ";
        }
        for (Initial=2; Initial <= Second && Initial >0; Initial++)
        {
            cout << Initial << " ";
        }
        cout << endl;
    }
    Initial--; // This line doesn't do anything
}
So Saxy I Jazzed
(14 items)
 
  
CPUMotherboardGraphicsRAM
IBM i7 2600k Gigabyte P8P67 Deluxe Sapphire GTX 580 SC Mushkin Ripjaw 2x4GB 
Hard DriveOptical DriveCoolingOS
Crucial X25-M 120GB SSD + 1TB Seagate Caviar Black '00 Honda Civic Corsair CNPS 9900 MAX-B Mac OS7 Ultimate x64 
MonitorKeyboardPowerCase
LG E2360 overclocked to 120Hz Ultimate Nerd Cred Clicky Blanks Antec AX750 Cooler Master PC-B25FWB 
MouseMouse Pad
Razer G9x My desk 
  hide details  
Reply
So Saxy I Jazzed
(14 items)
 
  
CPUMotherboardGraphicsRAM
IBM i7 2600k Gigabyte P8P67 Deluxe Sapphire GTX 580 SC Mushkin Ripjaw 2x4GB 
Hard DriveOptical DriveCoolingOS
Crucial X25-M 120GB SSD + 1TB Seagate Caviar Black '00 Honda Civic Corsair CNPS 9900 MAX-B Mac OS7 Ultimate x64 
MonitorKeyboardPowerCase
LG E2360 overclocked to 120Hz Ultimate Nerd Cred Clicky Blanks Antec AX750 Cooler Master PC-B25FWB 
MouseMouse Pad
Razer G9x My desk 
  hide details  
Reply
post #4 of 4
Also here's a version I made. You can run it here.
I'd refer you to StackOverflow for programming questions, but they'll probably crucify you for posting homework questions.

Warning: Spoiler! (Click to show)
Code:
#include <iostream>
#include <iomanip>
using namespace std;

void printPyramid(int final);

int main() {
        int final;

        cout << "Please enter an integer from 1 to 15: ";
        cin >> final;

        if (cin.fail()) { // ALWAYS check for errors
                cerr << "Invalid input" << endl;
                return -1;
        }
        if (final < 1 || final > 15) {
                cerr << "Number must be between 1 and 15" << endl;
                return -1;
        }
        cout << endl; // (not really needed)

        printPyramid(final);
}

void printPyramid(int final) {
        int width = (final < 10) ? 2 : 3; // num over 10 need column size of 3

        for (int row = 1; row <= final; row++) {
                // Print spaces.
                // There are final - row columns before the first number
                for (int col = 1; col <= final - row; col++) {
                        cout << setw(width) << " ";
                }

                // Prints numbers decending from row to 1
                for (int num = row; num > 0; num--) {
                        cout << setw(width) << num;
                }

                // Prints numbers ascending from 2 to row
                for (int num = 2; num <= row; num++) {
                        cout << setw(width) << num;
                }
                cout << endl;
        }
}

Edited by ____ - 2/26/16 at 4:28am
So Saxy I Jazzed
(14 items)
 
  
CPUMotherboardGraphicsRAM
IBM i7 2600k Gigabyte P8P67 Deluxe Sapphire GTX 580 SC Mushkin Ripjaw 2x4GB 
Hard DriveOptical DriveCoolingOS
Crucial X25-M 120GB SSD + 1TB Seagate Caviar Black '00 Honda Civic Corsair CNPS 9900 MAX-B Mac OS7 Ultimate x64 
MonitorKeyboardPowerCase
LG E2360 overclocked to 120Hz Ultimate Nerd Cred Clicky Blanks Antec AX750 Cooler Master PC-B25FWB 
MouseMouse Pad
Razer G9x My desk 
  hide details  
Reply
So Saxy I Jazzed
(14 items)
 
  
CPUMotherboardGraphicsRAM
IBM i7 2600k Gigabyte P8P67 Deluxe Sapphire GTX 580 SC Mushkin Ripjaw 2x4GB 
Hard DriveOptical DriveCoolingOS
Crucial X25-M 120GB SSD + 1TB Seagate Caviar Black '00 Honda Civic Corsair CNPS 9900 MAX-B Mac OS7 Ultimate x64 
MonitorKeyboardPowerCase
LG E2360 overclocked to 120Hz Ultimate Nerd Cred Clicky Blanks Antec AX750 Cooler Master PC-B25FWB 
MouseMouse Pad
Razer G9x My desk 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Wondering if someone could help me real quick. C++ program