ENTER NUMBER OR LETTER and ASCENDING OR DESCENDING PROGRAM. PLS HELP

our professor gave us an assingment.
1. Choose what to enter NUMBER or LETTER.
2. Choose type of sorting ASCENDING or DESCENDING.

sorry guys i remember this type of problem when i was taking my class
3years ago and i forgot to take a note. my professor gave us this assignment,
actually 1st assignment of the semester. and i can't really find solution to this and im using turbo c++. so decided to ask you guys. hope you can help me.

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
#include<iostream.h>
#include<conio.h>
main()
{
int x,y,z;
cout<<"choose Number or letter\n1.number\n2.Letter";
cin>>x;

if(x==1)
{
cout<"Enter number";
}
else if(x==2)
{
cout<<"Enter letter";
}
cin>>y;

cout<<"Choose your sort type\n1.Ascending\n2.Descending";

??
??
??


getch();
return0;
}





note: this is the only code that i produce. i tried to look this problem in
my book, unfortunately it's not there. they say that i need to use array.
can someone help me guys urgently. thanks ^_^


closed account (o3hC5Di1)
Hi there,

Well I assume that the numbers or letters will have to be put into an array (or have you seen STL containers such as std::vector yet in your classes?).

So you will need to:

* Create an array
* Put a loop around the request for a letter or number so multiple values can be entered by the user
* Insert those values into the array

Now here's where you can do several things.
If you use a plain array, you will need a sorting algorithm (just google c++ sorting algorithm) in order to sort the array. If you use an STL container, you can actually insert the items in-place and keep the container sorted all the time:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <list>
#include <algorithm>

//std::list is an unsorted sequential container allowing fast insertion at any point
//I'm assuming the user chose to enter numbers:
std::list<int>  l;
while (y != -1) // -1 exits the loop for entering numbers
{
    cout<"Enter number";
    cin>>y;
    // insert the value in the right spot, use upper_bound() for descending order
    l.insert(std::lower_bound(l.begin(), l.end(), y), y); 
}


You could also use std::map or std::set, which are sorted containers (meaning that they auto-sort themselves whenever you insert a value), but the above should be faster for smaller datasets.

If you have to use regular arrays, create one for both ints and char's:

1
2
int number_arr[10]; //creates an array holding 10 ints
char character_arr[10]; //creates an array holding 10 chars 


Use a for loop to easily access the individual elements and prevent going out of range:

1
2
3
4
5
for (int i=0; i < 10; i++)
{
    //ask number y
    number_arr[i] = y;
}


Then figure out a way to sort it. One option is to use the bubble sort algorithm explained here:http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/

Sorry I tried to evade giving you a straight solution, but that's how you'll learn :)
If you have any further questions, please do let us know.

All the best,
NwN
thanks sir. but it's hard for me to understand about sorting algo/bubble sort.(the first code)
we didn't tackle about that STL, std::.first time to know it. but i try to understand(sorry)
I think it's easier for me
to use regular arrays and loop rather than that (i don't know if they are similar to each other
that's why i really need a help). sir can you give me a hint or a little short straight solution :))
i search on google. and i also read those bubble sort. is there a another solutin :)).
one last question sir. did I need to use switch case or if else in this?.

Sorry, a lot question, i'm still new in c++, thanks for your patience

closed account (o3hC5Di1)
Hi there,

No problem, we'll take it step by step:

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
#include<iostream.h>
#include<conio.h>

//this will make it clear what the maximum array size is
#define MAX_ARR_SIZE 10

main()
{
    int x,y,z;
    int number_array[MAX_ARR_SIZE];
    char character_array[MAX_ARR_SIZE];

    cout<<"choose Number or letter\n1.number\n2.Letter";
    cin>>x;

    //This will ask the user for values until the array is full
    //Note that we don't check if the input is indeed a number or character, 
    //but that can be implemented later
    for (int i=0; i<MAX_ARR_SIZE; i++)
    {
        if(x==1)
        {
            cout<"Enter number";
            cin >> number_array[i];
        }
        else if(x==2)
        {
            cout<<"Enter letter";
            cin >> character_array[i];
        }
    }

    cout<<"Choose your sort type\n1.Ascending\n2.Descending";

    //sort the array
    //output the array to std::cout

getch();
return0;
}


More information about how the array operations done in this code work, here:
http://cplusplus.com/doc/tutorial/arrays/

Have you not seen any sorting algorithms in class, I assume your professor wouldn't ask for them if you don't know about them?

All the best,
NwN
Last edited on
I only remember we use ascending and descending using Visual Basic.net, and for me I didn't experience more complex using VB compare to c++, but i really want to use c++ to learn more, the reason why i choose it. i understand your code sir,except that sort, it's killing me @_@. anyway, i'm still reviewing those links at this moment xD
Last edited on
closed account (o3hC5Di1)
Let's have a look at the bubble sort algorithm as described in the article linked to earlier (without optimization):

for each element in the sequence (except for the last element):
if the element and the next element need swapping:
swap them
repeat if any swaps were made


Now let's break that down:

for each element in the sequence (except for the last element):


This would translate into C++ as:

1
2
3
4
for (int i=0; i < MAX_ARR_SIZE-1; i++)
{
   // ...
}


if the element and the next element need swapping: swap them


Means: if the current element is smaller or larger than the next (depending on descending or ascending order respectively), so:

1
2
3
4
5
6
7
for (int i=0; i < MAX_ARR_SIZE-1; i++)
{
   if (num_array[i] > num_array[i+1])  //example for ascending
   {
        //swap the values of num_array[i] and num_array[i+1]
   }
}


repeat if any swaps were made


So we'll need a way of tracking whether elements were swapped or not, a boolean value sounds perfect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool swap = true;
while (swap == true)
{
    swap = false;

    for (int i=0; i < MAX_ARR_SIZE-1; i++)
    {
       if (num_array[i] > num_array[i+1])  //example for ascending
       {
            //swap the values of num_array[i] and num_array[i+1]
            swap = true;  //swap will only be true when values are actually swapped
       }
    }
}



That should get you started, best way would be to wrap this functionality into an asc_sort() and desc_sort() functions, but I'll leave that up to you :)

All the best,
NwN

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
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int numb[7];
int i,j,sort_type;

for(i=0;i<=6;i++)
{
  cout<<"Enter Numbers: ";
  cin >> numb[i];
}
cout<<"Choose your type of Sorting"<<endl
<<"1. Ascending"<<endl<<"2. Descending"<<endl;
cin>>x;
if(sort_type==1)
{
for(i=0;i<=5;i++)
{
  for(j=i+1;j<=6;j++)
  {
    int temp;

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

  for(i=0;i<=6;i++)
  {
     cout<<endl<<numb[i];
     }
      }

else if(sort_type==2)
{
for(i=0;i<=5;i++)
  {
     for(j=i+1;j<=6;j++)
     {
       int temp;
      if(numb[i] < numb[j])
  {
    temp = numb[i];
    numb[i] = numb[j];
    numb[j] = temp;
    }
   }
  }
    for(i=0;i<=6;i++)
    {
      cout<<endl<<numb[i];
	}
      }

  getch();
  return 0;
  }



the only problem is I don't get it how to put conditional statement on choosing
number or letter.
This is what i've done last night. but it's ok.
thanks for sharing your knowledge, i learn a lot ^_^
Last edited on
closed account (o3hC5Di1)
Hi there,

You would have to add a char array and do the same as you do for asking about the sorting:
Ask the user to enter 1) numbers, 2) characters, according to that you store the values in the numbers or characters array.

It may also help you to give your variables more descriptive names.
"x" for instance could better be called "sort_type", it will make the code more readable.

All the best,
NwN
should I add 1 more variable to use on if else in choosing num or letter?
can you give a a statement on that line? :)).
Last edited on
closed account (o3hC5Di1)
It would be around line #8 in your code and would look almost exactly like this part of your code, which does almost the same, just to select between ascending or descending:

1
2
3
4
cout<<"Choose your type of Sorting"<<endl
<<"1. Ascending"<<endl<<"2. Descending"<<endl;
cin>>x;  //by the way, don't forget to change x to sort_type here as well, or you'll get compiler errors.
if(sort_type==1)


All the best,
NwN
Topic archived. No new replies allowed.