Unscramble This Code

This is a discussion on Unscramble This Code within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> } current = current->next; i++; #include <cstdlib> float highest = -10000; string name; } void printHighest(player* head) ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Unscramble This Code

    Code:
    #include <iostream>
    		}
    		current = current->next;
    		i++;
    #include <cstdlib> 
    	float highest = -10000;
    	string name;
    }
    
    void printHighest(player* head)
    		{
    			current->next = NULL;
    		}
    		else
    	int i = 0;
    	while( i < 10 )
    	{
    {
    
    using namespace std;
    		cout << "Setup player: " << current->name << " with average: " << current->average << endl;
    	cout << current->name << endl;
    		
    	while (current->next != NULL)
    	{
    		if (i == 9)
    struct player{
    	string name;
    		}
    		current = current->next;
    	}
    	float average;
    	player* next;
    };
    	player* current = head;
    
    		
    		{
    
    			name = current->name;
    }
    			cout << "Player: " << current->name << " has taken over as highest" << endl;
    			highest = current->average;
    	printHighest(head);
    		{
    			current->next = new player;
    	return 0;
    void printHighest(player* head);
    cout << "Highest average is: " << name << endl;	
    	player* head = new player;
    player* current = head;
    		current->name = playerNames[i];
    		current->average = rand()%777;
    cout << "current->average is : " << current->average << endl;
    		if(current->average > highest)
    int main(void)
    {
    	string playerNames[] = {"mike","tom","derek","justin","brad","bob","jimbob","clemens","sarah","billy","jackson","hwee"};
    			}
    im so confused, basically have to define a struct that stores name and average. create a linked list, a function that takes head of hte list and prints the name with highest average. Any and all help is greatly appreciated, this scramble of code is confusing lol
    Last edited by ShiroAzure; 12-15-2011 at 07:42 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    The Output should be.
    Code:
    	Setup player: mike with average: 490
    	Setup player: tom with average: 7
    	Setup player: derek with average: 569
    	Setup player: justin with average: 587
    	Setup player: brad with average: 517
    	Setup player: bob with average: 398
    	Setup player: jimbob with average: 450
    	Setup player: clemens with average: 743
    	Setup player: sarah with average: 50
    	Setup player: billy with average: 400
    	mike
    	current->average is : 490
    	Player: mike has taken over as highest
    	current->average is : 7
    	current->average is : 569
    	Player: derek has taken over as highest
    	current->average is : 587
    	Player: justin has taken over as highest
    	current->average is : 517
    	current->average is : 398
    	current->average is : 450
    	current->average is : 743
    	Player: clemens has taken over as highest
    	current->average is : 50
    	Highest average is: clemens
    I know that the includes have to be at the top as does the using namespace std; followed by the struct i belive.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Wait, you're trying to say that this is a homework exercise where the lines of a working program were scrambled and you are supposed to unscramble them?
    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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    You clearly haven't made any effort of your own yet. Do as much as you can before posting here.
    You can at least make sure that both #includes are at the top, probably followed by any using statement.

    Make no mistake, you're the one doing the assignment, we'll only give you a few nudges along the way.
    Repost your best guess as to how it should go. Spend at least 30 minutes on it too, don't give up after 5.
    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"

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    struct player{
            string name;
            float average;
            player* next;
    }
    
    void printHighest(player* head);
    
    int main(void)
    {
            player*head = new player;
            player* current = head;
            string playerNames[] =
    {"mike","tom","derek","justin","brad","bob","jimbob","clemens","sarah","billy","jackson","hwee"};
    
    void printHighest(player* head);
    {
            player* current = head;
            string name;
    	      if(current->average > highest)
    	   {
    	current->next = NULL;
    	cout << "Highest average is: " << name << endl;	
    	player* head = new player;
    	player* current = head;
    
    	   }
    		else
    	int i = 0; 
    }
    return 0;
    
    }
    i got this far after a few hours of headache and im still lost :P

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The next thing you need is a function to add a player to the list. IMO, it is a mistake to have written "printHighest" first, because there is no way for you to use it without a populated list.

    Probably it is not necessary to create a new player struct in main() for the head, see below.

    Do you know how to write classes yet? The list might be easier to manage that way. If not, don't worry. Just write an "addPlayer" function that has a prototype something like this:

    Code:
    player *addPlayer (player *head, string &name, float average);
    In this you are going to create a new player struct, set the correct values, and make it's "next" pointer the head. For the first player, submit NULL for head, ie, you have a loop in main that looks like this:

    Code:
    player *head = NULL;
    
    while ([more players to insert) {
         head = addplayer(head, ..., ...);
    }
    Last edited by MK27; 12-16-2011 at 09:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    You seem to have lost a few lines. Put those in there even if they are all just at the end so that we can see which lines still need to be added in.

    One of the main things to fix at the moment is that you can't have a function inside a function.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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