# Arrays and Loops

• 09-07-2011
jawz932
Arrays and Loops
I am required to enter 10 stocks with prices and names. I must use a loop to create 2 separate arrays and then sort them. I have the bubblesort down, but the arrays and loop is messing with me for some reason! Any help is greatly appreciated.
• 09-07-2011
manasij7479
How can you manage a bubble sort and still have problems with arrays and loops ?
• 09-07-2011
jawz932
I have no clue man? Ok, here is what I have so far. I'm not completed with the entire program, but I'm having problems with the arrays. I guess maybe I've been looking at it to long.

Code:

``` #include <iostream> #include <math.h> using namespace std; int main(){ char stockname; int stocks[10]; int average; int total; char totalname; int left=0; int right=9; int menu_selection; int case1; int case2; int case3; int case4; int case5; char wait='1'; stocks[0]; int i; int a; do{ cout<<"Input the names and prices of 10 differnt stocks"<<endl<<endl; for(i=0; i<10; i++) {         {                 for(a=0; a<10; a++)                 {         cout<<"Enter the cost of stock #"<<i+1<<": ";         cin>>stocks[i];                         {                                 cout<<"Enter the name of stock #"<<i+1<<": ";                                 cin>>stockname[a];                         {                                 total=(stockname[a],stocks[i]);                                 return 0;                         }                         }                 }         } }                         cout<<endl<<endl; cout<<"(1. Convert from Dollars to Pounds."; cout<<"\n(2. Convert from Dollars to Euro's."; cout<<"\n(3. Average the prices of the 10 stocks."; cout<<"\n(4. Count up how many stocks are above \$25."; cout<<"\n(5. Display the stocks sorted."<<endl<<endl; cout<<"Enter your selection from the menu: "; cin>>menu_selection; cout<<endl<<endl; cout<<"You entered "; switch (menu_selection){ case 1:         cout<<"\n(1. Convert from Dollars to Pounds."<<endl<<endl;         cout<<"The stocks converted are "<<endl;         cout<<stocks[0]/.97<<" ponds."<<endl;         cout<<stocks[1]/.97<<" ponds."<<endl;         cout<<stocks[2]/.97<<" ponds."<<endl;         cout<<stocks[3]/.97<<" ponds."<<endl;         cout<<stocks[4]/.97<<" ponds."<<endl;         cout<<stocks[5]/.97<<" ponds."<<endl;         cout<<stocks[6]/.97<<" ponds."<<endl;         cout<<stocks[7]/.97<<" ponds."<<endl;         cout<<stocks[8]/.97<<" ponds."<<endl;         cout<<stocks[9]/.97<<" ponds."<<endl;         break; case 2:         cout<<"(2. Convert Dollars to Euros's";         cout<<"\n1. Convert from Dollars to Euro's."<<endl<<endl;         cout<<"The stocks converted are "<<endl;         cout<<stocks[0]/.45<<" euros."<<endl;         cout<<stocks[1]/.45<<" euros."<<endl;         cout<<stocks[2]/.45<<" euros."<<endl;         cout<<stocks[3]/.45<<" euros."<<endl;         cout<<stocks[4]/.45<<" euros."<<endl;         cout<<stocks[5]/.45<<" euros."<<endl;         cout<<stocks[6]/.45<<" euros."<<endl;         cout<<stocks[7]/.45<<" euros."<<endl;         cout<<stocks[8]/.45<<" euros."<<endl;         cout<<stocks[9]/.45<<" euros."<<endl;         break; case 3:         cout<<"(3. Average price of all 10 stocks.";         average=(stocks[0]+stocks[1]+stocks[2]+stocks[3]+stocks[4]+stocks[5]+stocks[6]+stocks[7]+stocks[8]+stocks[9])/10.0;         cout<<endl;         cout<<"\n The average price of all 10 stocks is: \$"<<average;         break; case 4:         cout<<"(4. Number of stocks above \$25,"<<endl<<endl;         break; case 5:         cout<<"(5. Stocks sorted.";         cout << endl << "Stocks after being sorted: "<<endl<<endl;                 for (int i = left; i < right; i++){                 for (int j = right; j > i; j--){                         if(stocks[j] < stocks[j-1]){                                 int temp = stocks[j];                                 stocks[j] = stocks[j-1];                                 stocks[j-1] =temp;                         }                 }         }                                for (int i=0; i<10; i++){                 cout << " \$"<< stocks[i];         }                 break; cout<<endl<< "\nEnter X to exit, any other key to continue: ";                 cin >> wait; } while(wait != 'x' && wait != 'X'); return 0;   } }```
• 09-07-2011
jawz932
I would greatly appreciate any help. This is for a class I am taking online. We are in the 7th or 8th week and have had little to no instruction, so I have learned most of this all on my own.
• 09-07-2011
grumpy
Try fixing your indenting. At present, the indentation does not reflect what the code does. Compilers ignore indenting, but fixing the indenting will help you understand what the code is actually doing.

Also, when code doesn't work (or triggers compiler errors) cutting and pasting chunks of code willy-nilly is rarely a way to fix the problem.
• 09-07-2011
anon
Quote:

How can you manage a bubble sort and still have problems with arrays and loops ?
It is rather hard to imagine that this

Code:

```for (int i = left; i < right; i++){                 for (int j = right; j > i; j--){                         if(stocks[j] < stocks[j-1]){                                 int temp = stocks[j];                                 stocks[j] = stocks[j-1];                                 stocks[j-1] =temp;                         }                 }```
and this
Code:

```        cout<<stocks[0]/.45<<" euros."<<endl;         cout<<stocks[1]/.45<<" euros."<<endl;         cout<<stocks[2]/.45<<" euros."<<endl;         cout<<stocks[3]/.45<<" euros."<<endl;         cout<<stocks[4]/.45<<" euros."<<endl;         cout<<stocks[5]/.45<<" euros."<<endl;         cout<<stocks[6]/.45<<" euros."<<endl;         cout<<stocks[7]/.45<<" euros."<<endl;         cout<<stocks[8]/.45<<" euros."<<endl;         cout<<stocks[9]/.45<<" euros."<<endl;```
could be written by the same person.
• 09-07-2011
jawz932
I'm not making excuses, but we have had no instruction in this class. Literally, the professor has given us stuff to copy over from him with no guidance. I figured this was a good place to come to get ideas or guidance from experts, or at least someone who knows a little about it. I wasn't looking for any negative criticism.
• 09-07-2011
manasij7479
Quote:

Originally Posted by jawz932
I'm not making excuses, but we have had no instruction in this class. Literally, the professor has given us stuff to copy over from him with no guidance. I figured this was a good place to come to get ideas or guidance from experts, or at least someone who knows a little about it. I wasn't looking for any negative criticism.

What you figured to be negative criticism is actually somewhat positive.
It simply means that, if you've not got the basics of programming down, you SHOULD get a good book/tutorial and start from scratch.
Trying to write half-cooked programs and then copying half of the code can potentially harm you much more than you can imagine.
• 09-07-2011
jawz932
What is a good book on programming? I'm not exactly into the c++ programming, I'm really wanting to get into android and java, which, if I understand right, are not that much different. I was required to take this class through my school, so I had to start someplace.
• 09-07-2011
manasij7479
Quote:

Originally Posted by jawz932
What is a good book on programming? I'm not exactly into the c++ programming, I'm really wanting to get into android and java, which, if I understand right, are not that much different. I was required to take this class through my school, so I had to start someplace.

Don't count of anything being 'not much different'.

Look into the recommended books section of this board.
Also,
Accelerated C++ and Thinking in C++ (Vol 1) is a good place to start.
• 09-08-2011
iMalc
Well if you want us to help you to fix your program then you're going to have to tell us what you see as being the problem, and ask a question about the specific thing that you're having trouble with.
• 09-08-2011
AndrewHunter
What you really need to do, as has been already mentioned is to sit down and learn the language. Online course or not I am sure there is a book required. In the meantime you may find some advantage in looking over:
These all come from CProgramming's Tutorials. I suggest that you read through all those lessons to get your level of knowledge up to where it needs to be to complete this class. Read through these tutorials, rework your program and then repost with any problems you have.