Array help on my quiz

This is a discussion on Array help on my quiz within the C++ Programming forums, part of the General Programming Boards category; Hey guys, so I just recently got back into programming and I decided to program one of those stupid "what ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    Array help on my quiz

    Hey guys, so I just recently got back into programming and I decided to program one of those stupid "what highschool stereotype are you" quizzes to see what i still remember. Everything went great untill I realized that I don't know how to check if one variable is the greatest out of all the variables in the program (which would decide the end result) without writing a ton of if statements. I asked someone how i could do it in a simpler way and they said I need to put my variables in an array so I could bubble sort them. So my question is how do I make an array with 8 int variables in it and also if you could help me out with the whole bubble sort thing that would be great. Here's my program so far if it helps (im only putting in the first part cuz its pretty long but you should get the gist of what I'm trying to do)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int think;
    	int bro = 0;
    	int jock = 0;
    	int art = 0;
    	int rock = 0;
    	int stoner = 0;
    	int nerd = 0;
    	int rich = 0;
    	int ba = 0;
    	int a;
    	int b;
    	int c;
    	int d;
    	int e;
    	int f;
    	int g;
    
    	cout<<"THE HIGHSCHOOL STERIOTYPE QUIZ\n";
    	cout<<"\n";
    	cout<<"How To Play:\n";
    	cout<<"You will be asked a series of questions to determine your personality, \nanswer truthfully by typing the number next to the response that fits you best \nand then hitting the enter key. In the event that multiple responses fit you, \nDO NOT type both numbers, choose the one that fits you best.\n";
    	cout<<"\n";
    	cout<<"Are you ready to find out what highschool steriotype you are? \nPress enter to find out!\n";
    	cin.get();
    
    	cout<<"What would you classify yourself as? (does not count towards final result)\n";
    	cout<<"\n";
    	cout<<"1. Lax Bro\n";
    	cout<<"2. Jock\n";
    	cout<<"3. Artist\n";
    	cout<<"4. Rocker\n";
    	cout<<"5. Stoner\n";
    	cout<<"6. Nerd\n";
    	cout<<"7. Rich Kid\n";
    	cout<<"or are you...\n";
    	cout<<"8. BEYOND AUTHORITY!!!\n";
    	cout<<"\n";
    	cin>> think;
    	cout<<"\n";
    
    	cout<<"QUESTION 1\n";
    	cout<<"\n";
    	cout<<"What's your favorite sport?\n";
    	cout<<"\n";
    	cout<<"1. Football\n";
    	cout<<"2. Do gigs count as sports?\n";
    	cout<<"3. Baseball\n";
    	cout<<"4. Lacrosse\n";
    	cout<<"5. Sports?\n";
    	cout<<"6. I'm too good for sports\n";
    	cout<<"\n";
    	cin>> a;
    	cout<<"\n";
    
    	switch (a) {
    		case 1:
    			jock = jock + 1;
    			break;
    		case 2:
    			rock = rock + 1;
    			break;
    		case 3:
    			rich = rich + 1;
    			break;
    		case 4:
    			bro = bro + 1;
    			break;
    		case 5:
    			art = art + 1;
    			nerd = nerd + 1;
    			stoner = stoner + 1;
    			break;
    		case 6:
    			ba = ba + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Who ever told you to bubble sort an array to find the largest integer needs to take some of those quizzes for their self.

    1) Put the variables in to an array
    2) loop through the array (for, while, do while, you choose)
    3) compare each number to a "max" variable, if bigger than replace "max" with the new high

    Forget about sorting in general, it serves no purpose for your task.

    Code:
    int variables[8];
    
    for(int i = 0; i < 8; i++) {
        variables[i] = i; // or what ever value you want
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ok thanks a lot. I do have a couple questions though cuz i kinda suck at programming haha. in the 3rd line of your code what does that value do? In the example on this site the value there is x*y but i dont want the array to really do anything except for let me compare the variables so what would I put there? Also, as you can see, My program has more variables than the ones I want to compare so how would i tell the array to hold only the variables I want to compare? Thirdly, I don't understand what you mean about a max variable. Thanks.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    by max variable he means make a variable that stands for the biggest number you have found so far in the loop.

    Code:
    int max = 0;
    for( i=0;  .......... )
    if( arr[i] > max )
    max = arr[i]

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by dyelax View Post
    in the 3rd line of your code what does that value do?
    The code I posted simply just shows how to fill an array with values, in this case it would store 0 at variable[0], 1 at variable[1], etc.

    Also, as you can see, My program has more variables than the ones I want to compare so how would i tell the array to hold only the variables I want to compare?
    Just assign the value to some index of the array, like I did with the loop and putting variable[i] = i;

    Thirdly, I don't understand what you mean about a max variable. Thanks.
    Exactly what rodrigorules said.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    so for mine would I do something like variables[0] = bro, variables[1] = jock etc? and i still dont get the whole max variable thing

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by dyelax View Post
    so for mine would I do something like variables[0] = bro, variables[1] = jock etc?
    Yes.

    and i still dont get the whole max variable thing
    What don't you get? How would you do this problem if you had pen and paper, and a list of things to go through? (What if the list was 10000000000 numbers?)

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    sorry for bringing back this thread but i gave up on this program for a little. I really dont understand how i would make a max variable or even implement it in here

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by dyelax View Post
    sorry for bringing back this thread but i gave up on this program for a little. I really dont understand how i would make a max variable or even implement it in here
    Just store the values of "jock", "rock", blah, blah, in a map with the following definition:

    Code:
    std::map <int, string> classOfPerson;
    Next, iterate through the int side of the map, and find the greatest number. Then output a message saying you're this one (i.e. the corresponding string value).

    http://www.cplusplus.com/reference/stl/map/
    Last edited by Programmer_P; 06-12-2010 at 11:56 AM. Reason: added [code][/code] tags
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Ummm, that's silly. That's just a waste of time.
    Just use std::max_element: max_element [C++ Reference]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    alright Elysia im taking your advice and im doing something wrong haha. heres the code:

    Code:
    #include <algorithm>
    
    foreward_iterator max_element (foreward_iterator start, foreward_iterator end);
    
    
    // then the rest of the main() which works fine and then this:
    
    
    		int ar[8] = { bro,jock,art,rock,stoner,nerd,rich,ba };
            unsigned int array_size = sizeof(array) / sizeof(array[0]);
            cout<<"You are really a "
    
    			if (*max_element(array, array+array_size) = bro){
    				cout<<"lax bro!\n";
    			}
    
    			if (*max_element(array, array+array_size) = jock){
    				cout<<"jock!\n";
    			}
    
    			if (*max_element(array, array+array_size) = art){
    				cout<<"artist!\n";
    			}
    
    			if (*max_element(array, array+array_size) = rock){
    				cout<<"rocker!\n";
    			}
    
    			if (*max_element(array, array+array_size) = stoner){
    				cout<<"stoner!\n";
    			}
    
    			if (*max_element(array, array+array_size) = nerd){
    				cout<<"nerd!\n";
    			}
    
    			if (*max_element(array, array+array_size) = rich){
    				cout<<"rich kid!\n";
    			}
    
    			if (*max_element(array, array+array_size) = ba){
    				cout<<"BEYOND AUTHORITY!!!\n";
    I know im really bad but this is the best i could do looking at the link you sent me. what am I doing wrong?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    also as I've already declared my array like this its saying I'm redefining it by doing that^

    Code:
    int ar [8];
    
    	for (int i = 0; i < 8; i++) {
    		ar[0] = bro;
    		ar[1] = jock;
    		ar[2] = art;
    		ar[3] = rock;
    		ar[4] = stoner;
    		ar[5] = nerd;
    		ar[6] = rich;
    		ar[7] = ba;
    	}

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Remove this line:
    Code:
    foreward_iterator max_element (foreward_iterator start, foreward_iterator end);
    What you need is to #include <algorithm>, which you have already done.

    Next, you only need to call std::max_element once. Once you get the max element, compare it with the other values, but it does not make sense to compare them with those variables. Use == for equality comparison.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ok. could you give me an example of how i would do both of those things?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by dyelax
    ok. could you give me an example of how i would do both of those things?
    I would suggest something like this:
    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    // ...
    
    int stereotypes[] = { bro, jock, art, rock, stoner, nerd, rich, ba };
    size_t num_stereotypes = sizeof(stereotypes) / sizeof(stereotypes[0]);
    int* player_stereotype = max_element(stereotypes, stereotypes + num_stereotypes);
    string stereotype_names[] = {"lax bro", "jock", "artist", "rocker",
                                 "stoner", "nerd", "rich kid", "BEYOND AUTHORITY"};
    cout << "You are really a " << stereotype_names[player_stereotype - stereotypes] << "!\n";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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