Like Tree4Likes
  • 1 Post By manasij7479
  • 3 Post By anon

Arrays and Loops

This is a discussion on Arrays and Loops within the C++ Programming forums, part of the General Programming Boards category; I am required to enter 10 stocks with prices and names. I must use a loop to create 2 separate ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Lightbulb 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.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    How can you manage a bubble sort and still have problems with arrays and loops ?
    Salem likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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;
      }
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    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.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by jawz932 View Post
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by jawz932 View Post
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Loops
    By sunandstars in forum C Programming
    Replies: 1
    Last Post: 02-17-2011, 03:34 PM
  2. help with arrays and loops
    By pmooney12 in forum C Programming
    Replies: 48
    Last Post: 11-27-2010, 09:43 AM
  3. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  4. while loops with arrays
    By volk in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 06:22 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21