Solved

Need help with this small program using an array.

Posted on 2006-11-08
6
179 Views
Last Modified: 2010-04-24
Hi Experts!,

I'm making this small program that generates 3 randon integers and displays them.
Then it's suppose to sort them and display them letting me know which one is in what element.
I've been working on this trying to follow some book examples, but I'm very lost in the function that sorts them so I can display them back in MAIN()
I must have left out a couple of things or not put the correct commands obviously, if you could please let me know what and where I need to change things.
I want to do this in the most basic way possible, not using other sophisticated ways or commands, cause I'm just learning this as of now.

Thank you for your help.

#include <iostream>
using namespace std;

unsigned int numarray[3];


int RandomNumber(void)

{
   
    int a;
   
    srand(14);
   for(a = 0; a < 3; a++)
   {
       numarray[a] = rand()% 3;
   }
   return 0;
}
int sortArray(void)
{
      int temp, elems = 0;
      bool swap;

      do
      {
            swap = false;
            for (int count = 0; count < (elems - 1); count++)
            {
                  if (numarray[count] > numarray[count +1])
                  {
                        temp = numarray [count];
                        numarray[count] = numarray[count+1];
                        numarray[count +1] = temp;
                        swap = true;
                  }
            }
      } while (swap);

      return 0;

}
int main (void)
{
    int value;
    value=RandomNumber();
      cout << numarray[0] <<"  "<< numarray[1] <<"  " << numarray[2] << endl;
      sortArray();
      cout << "\nThe 1st element is " << numarray[0];
      cout << "\nThe 2nd element is " << numarray[1];
      cout << "\nThe 3rd element is " << numarray[2];
      system ("pause");
    return 0;
}
0
Comment
Question by:manz2
  • 4
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17903698
>for (int count = 0; count < (elems - 1); count++)

elems was initalized to 0 ... so this loop will never be entered ... Also the do while loop will exit immediately since you just set the swap flag to false!!!

On a side note
numarray[a] = rand()% 3; ... why mod 3?

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17903707
elems should have been intialized to number of elements in the array
0
 

Author Comment

by:manz2
ID: 17903904
> numarray[a] = rand()% 3; ... why mod 3?

Is it not necessary to make the range of numbers between 0 - 4 ??

I just changed it right now to:

numarray[a] = 1 + rand()% 4;

so that it ranges between 1 -4

correct? or not?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17903953
%4 will give you range of 0 to 3 ... Why do you need to limit your random numbers? They should be printed and sorted fine even without the restriction ... I hope you are not confusing array indices with the random numbers which are stored at those indices :)
0
 

Author Comment

by:manz2
ID: 17904000
These are my actual instructions for the program.

Generate an array of 50 random integers between 0-100. Store the number in a 1-d array and
then sort it. Using binary search, allow the user to input a number and then find if the number
is in the stored array. If it is in the array, then display the first location of that number.

But I decided to start coding one step at a time to see how everything is works along the way.

Eventually I will have to change the random generator from 3 to 50, then I have to work
on adding the binary search part and then displaying the position of the integer.

As of right now the part of the program I listed is working, thanks to you, now I have to add the rest.

If I run into problems, I'll ask again on the site, thanks again sunnycoder! :)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17904007
Glad to assist :) ... Good luck!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now