d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Noob Question
12Next
Add Reply New Topic New Poll
Member
Posts: 3
Joined: Oct 5 2013
Gold: 0.00
Oct 5 2013 12:49pm
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;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 5 2013 01:10pm
Quote (carteblanche @ Oct 5 2013 02:34pm)
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.


post if you need more help, and be specific with what problem you're facing.
Member
Posts: 3
Joined: Oct 5 2013
Gold: 0.00
Oct 5 2013 01:23pm
Hello,

I've only been programming for about 1 month so im kinda noob :P i dont really understood what you wrote in the other thread. The thing i would like go get help with is the first example;

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

Perhaps some code so i can see how it works cus im kinda lost right now :(.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 5 2013 01:44pm
are you sorting the order they were entered or sorting the numbers themselves? sorting is done the exact same way, whether it's in increasing or decreasing order.

heres an example for selection sort.



This post was edited by carteblanche on Oct 5 2013 01:45pm
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Oct 5 2013 01:48pm
Quote (Teee @ Oct 5 2013 02:23pm)
Hello,

I've only been programming for about 1 month so im kinda noob :P i dont really understood what you wrote in the other thread. The thing i would like go get help with is the first example;

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

Perhaps some code so i can see how it works cus im kinda lost right now :(.


In pseudo code:

Code
dim increase int = 0
dim decrease int = 0
For x=1 to len(integersarray) {
if integersarray(x) <= integersarray(x+ 1) {
increase = increase + 1;
}
if integersarray(x) >= integersarray(x + 1) {
decrease = decrease + 1;
}
}

if increase = len(integersarray) { echo Increases }
if decrease = len(integersarray) { echo Decreases }


This post was edited by 0n35 on Oct 5 2013 01:51pm
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 5 2013 02:34pm
Im currently on the same problem... -.-

My program does sort out smallest to biggest / biggest to smallest... but I dont know how to NOT sort out if the numbers are not in order. Since the program should just sort it when theyre in order and show the reverse order..

Code
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdio.h>

using namespace std;

int main()
{
int numb[10];
int temp;

for(int i = 0; i <=9; i++)
{
cout << "Please enter ten numbers: ";
cin >> numb[i];
}
for(int i=0;i<=9;i++)
{

for(int j = i+1; j <=9; j++)
{

if(numb[i] < numb[j])
{
temp = numb[i];
numb [i] = numb [j];
numb [j] = temp;


}
}
}
for(int i = 0; i <=9; i++)
{
cout << numb[i] << " ";
}
}


I also having a big problem with the first for loop.. it asks the same question "Please enter ten numbers" 10 times -.- when it just should be asked once. If I remove the for loop the program doesnt work properly.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 5 2013 02:41pm
Quote (bomben @ Oct 5 2013 04:34pm)
but I dont know how to NOT sort out if the numbers are not in order. Since the program should just sort it when theyre in order and show the reverse order..

i dont understand what your question is. can you rephrase? if you're asking how to keep a sorted copy + non-sorted copy, just copy the array so you have two of them, then only sort one.

Quote
I also having a big problem with the first for loop.. it asks the same question "Please enter ten numbers" 10 times -.- when it just should be asked once. If I remove the for loop the program doesnt work properly.


Move that line above the loop.
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 5 2013 02:49pm
^
I'll screenshot the PDF with the exercise.



I got trubble with "Example 4" if the text is not sorted "Lowest > highest" or "Highest < Lowest" all the way it should display "The sequence is not sorted". I can just sort highest to lowest no matter the order.

This post was edited by bomben on Oct 5 2013 02:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 5 2013 03:15pm
check if their list is sorted. if it's not, then display it's not sorted. not sure what your problem is.
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Oct 5 2013 03:32pm
Quote (carteblanche @ Oct 5 2013 04:15pm)
check if their list is sorted. if it's not, then display it's not sorted. not sure what your problem is.


this shit is simple smh
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll