d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Small Problem > Y
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 5 2013 08:17am
Hello, im currently trying to make a program that shows like this (green is the thing u type in)

"
Enter n: 9
1 --> 1
1+2 --> 3
1+2+3 --> 6
1+2+3+4 --> 10
1+2+3+4+5 --> 15
1+2+3+4+5+6 --> 21
1+2+3+4+5+6+7 --> 28
1+2+3+4+5+6+7+8 --> 36
1+2+3+4+5+6+7+8+9 --> 45

"

but my program shows it like this

"
Enter n: 9
1 --> 1
22 ---> 2
333 ---> 3
4444 --> 4
55555 --> 5
666666 --> 6
7777777 --> 7
88888888 --> 8
999999999 --> 9

"

Heres my code

Code
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{

int n = 0;
int value;



cout << "Enter n: ";
cin >> n;


for(int i = 1; i <= n; i++){


value = 0 + i;


for ( int j = 1; j <= value; j++ )

cout << value;
cout << " --> ";
cout << value;


cout << endl;
}
return 0;
}


Thanks for the help.
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Oct 5 2013 08:59am
Code
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{

int n = 0;
int total;

cout << "Enter n: ";
cin >> n;

for(int i = 1; i <= n; i++){

total = 0;

for( int k = 1; k <= i; k++){

total = total + k;

if( k != 1) cout << "+";
cout << k;

}

cout << " --> ";
cout << total;
cout << endl;

}

return 0;
}


Make sure to understand the loops correctly

This post was edited by ShadowFiend on Oct 5 2013 09:00am
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 5 2013 09:09am
^
Thank you sir, I tried with a second loop inside but it didnt work. But ur code does the job :). I might come back here for tips on my other exercises. Thank you again mate.
Member
Posts: 3
Joined: Oct 5 2013
Gold: 0.00
Oct 5 2013 12:24pm
Hello im kinda stuck with my program atm... I would really like some help :).

Write a program that performs the following steps. You do not need to use functions in this exercise.

1. Ask the user to type a sequence of 10 integers and then store the values in an array, by the same order as they were entered by the user.

2. If the user entered a sequence of values not sorted then display the message “Sequenceis NOT sorted”.

3. If the user entered a sequence of values sorted increasingly then display the user sequence by decreasing order.

4. If the user entered a sequence of values sorted decreasingly then display the user sequence by increasing order.

Some examples are given below.

Example 1
Enter ten integers: 1 2 3 4 5 6 7 8 9 10
Sequence decreasingly sorted: 10 9 8 7 6 5 4 3 2 1

Example2
Enter ten integers:10 1 1 -2 -3 -4 -5 -6 -7 -7
Sequence increasingly sorted:-7 -7 -6 -5 -4 -3 -2 1 1 10

Example3
Enter ten integers:2 2 2 2 2 2 2 2 2 2
Sequence increasingly sorted:2 2 2 2 2 2 2 2 2 2

Example4
Enter ten integers:1 2 6 3 8 10 -3 0 0 8
Sequence is NOT sorted


My code so far.
Code
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdio.h>

using namespace std;

int main()
{
int const SIZE =10;


double num[SIZE];
cout << "Enter ten integers: ";
for (int i =0; i <SIZE; i++)


cin >> num[i];
cout << endl;


for(int i =0; i <SIZE; i++)
{
cout << num[i] << setw(3);
}

return 0;
}


This post was edited by Teee on Oct 5 2013 12:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 5 2013 12:34pm
Quote (Teee @ Oct 5 2013 02:24pm)
Hello im kinda stuck with my program atm... I would really like some help :).

Write a program that performs the following steps. You do not need to use functions in this exercise.

1. Ask the user to type a sequence of 10 integers and then store the values in an array, by the same order as they were entered by the user.

2. If the user entered a sequence of values not sorted then display the message “Sequenceis NOT sorted”.

3. If the user entered a sequence of values sorted increasingly then display the user sequence by decreasing order.

4. If the user entered a sequence of values sorted decreasingly then display the user sequence by increasing order.

Some examples are given below.

Example 1
Enter ten integers: 1 2 3 4 5 6 7 8 9 10
Sequence decreasingly sorted: 10 9 8 7 6 5 4 3 2 1

Example2
Enter ten integers:10 1 1 -2 -3 -4 -5 -6 -7 -7
Sequence increasingly sorted:-7 -7 -6 -5 -4 -3 -2 1 1 10

Example3
Enter ten integers:2 2 2 2 2 2 2 2 2 2
Sequence increasingly sorted:2 2 2 2 2 2 2 2 2 2

Example4
Enter ten integers:1 2 6 3 8 10 -3 0 0 8
Sequence is NOT sorted


My code so far.
Code
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdio.h>

using namespace std;

int main()
{
int const SIZE =10;


double num[SIZE];
cout << "Enter ten integers: ";
for (int i =0; i <SIZE; i++)


    cin >> num[i];
    cout << endl;


    for(int i =0; i <SIZE; i++)
    {
    cout << num[i] << setw(3);
    }

return 0;
}


Make your own topic. i recommend you look into sorting algorithms. insertion sort or selection sort is probably easy enough for you.

to determine if it's already sorted just compare two consecutive indexes. iterate over the whole array.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll