• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

[SOLVED] c++ sorting problem

25 posts in this topic

I have been working on this problem for a while.I am working on writing a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people. My question how do I modify this program so that it outputs a list in order of number of pancakes eaten by all 10 people. I am using a bubble sort to sort the array of values.I also want to output which person ate the least and most number of pancakes, which I have figured out.

I want the program to output the following list.

Person 4: ate 10 pancakes.
Person 3: ate 7 pancakes.
Person 8: ate 4 pancakes.
...
Person 5: ate 0 pancakes.

Here is the code I am using.

[source lang =c++]

#include <iostream>
#include <algorithm>
using namespace std;
int array[10];
int main()
{
for(int i=0;i< 10;i++)
{
cout << "Enter number of pancakes eaten: ";
cin >> array[i];
}
for(int i=0; i<10; i++)
{
for(int j=0;j<i;j++)
{
if(array[i]<array[j])
{
int temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
int size=10;
int position = 0;
int result = -1;
int target=array[9];
while(result==-1 && position < size)
{
if(array[position] == target)
{
result = position;
}
position++;
}

cout << endl;
cout << "Person " << result << " ate " << array[result] << " the most pancakes." << endl;
cout << endl;
size=10;
position=0;
result = -1;
target = array[0];
while(result==-1 && position < size)
{
if(array[position] == target)
{
result = position;
}
position++;
}
cout << endl;
cout << "Person " << result << " ate " << array[result] << " the least pancakes." << endl;
cout << endl;
for (int j=0; j< 10; j++)
{
cout << "Person " << j << " ate " << array[j] << " pancakes." << endl;
}
int test;
cin >> test;
return 0;
}

[/source]
0

Share this post


Link to post
Share on other sites
I cannot see the whole code.

I didn't really get your problem. Getting min/max is easy when you have it sorted.
So: currently you sort the array of numbers (called array, bad thing, I have no idea what it should contain. neither do you after a few weeks), containing the number of pancakes i presume. Your problem is that you then cannot link the number to the person anymore?

If you are interested, have a look at the insertion sorting algorithm, it is more usable than bubble sort, if you want to implement a sorting algorithm yourself.
0

Share this post


Link to post
Share on other sites
#include <iostream>
#include <algorithm>
using namespace std;
int array[10];
int main()
{
for(int i=0;i< 10;i++)
{
cout << "Enter number of pancakes eaten: ";
cin >> array[i];
}
for(int i=0; i<10; i++)
{
for(int j=0;j<i;j++)
{
if(array[i]<array[j])
{
int temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
int size=10;
int position = 0;
int result = -1;
int target=array[9];
while(result==-1 && position < size)
{
if(array[position] == target)
{
result = position;
}
position++;
}

cout << endl;
cout << "Person " << result << " ate " << array[result] << " the most pancakes." << endl;
cout << endl;
size=10;
position=0;
result = -1;
target = array[0];
while(result==-1 && position < size)
{
if(array[position] == target)
{
result = position;
}
position++;
}
cout << endl;
cout << "Person " << result << " ate " << array[result] << " the least pancakes." << endl;
cout << endl;
for (int j=0; j< 10; j++)
{
cout << "Person " << j << " ate " << array[j] << " pancakes." << endl;
}
int test;
cin >> test;
return 0;
}

here is the whole code.
0

Share this post


Link to post
Share on other sites
[quote name='phil67rpg' timestamp='1348348533' post='4982732']
here is the whole code.
[/quote]
Please use [code ][/code ] tags (without the space). [source ] tags are broken on GameDev.net. Also, I think you're OP is better then they have been in the past, so good job on improving the quality. Just one more thing to do to improve it: you didn't explain exactly what the problem is. You did well explaining what you wanted to do and you're end goal, but it would've helped had you been clear about why it's not working, and what is currently happening.

Anyway... good to see you're coming back to this pancake problem of yours. As Brother Bob has pointed out, you need to pair the person with the number of pancakes they ate. If you don't pair them together, it's impossible to know exactly who ate how many pancakes.
0

Share this post


Link to post
Share on other sites
thanks corn, well the problem is that I am trying to sort the number of pancakes eaten not the person who has eaten them.I am sorry I am having difficult time explaining the problem.I will study up on oop and structs/classes.
0

Share this post


Link to post
Share on other sites
You should really learn to describe your problem better. If you can't explain it to humans, there is little hope of being able to explain the solution to a computer.

Perhaps you could give us an example of input and the expected output?
0

Share this post


Link to post
Share on other sites
The output should look like this:

Person 4: ate 10 pancakes.
Person 3: ate 7 pancakes.
Person 8: ate 4 pancakes.
...
Person 5: ate 0 pancakes.

The input looks like this:
for(int i=0; i<10;i++)
{
cout << "Enter the number of pancakes eaten: ";
cin >> array[i];
}

I can populate the array just fine, I am having minor in sorting the number of pancakes ate.
0

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1348350412' post='4982740']
Perhaps you could give us an example of input and the expected output?
[/quote]
And the actual (incorrect) output, so it's clear what the problem is.
0

Share this post


Link to post
Share on other sites
I think we all understood the problem just fine. You are not sorting numbers of pancakes; you are sorting people based on how many pancakes they ate. Brother Bob's suggestion is the easiest solution.
0

Share this post


Link to post
Share on other sites
wow that is alot to learn,I have decided to rewrite my program using classes and member funcitions,and the heap,it is some good practice for me,I am also using pointers.I like to oop techniques when I can but I have tenous understanding of pointers, but I am doing some studying up on them.here is the code I am using.
[code]
#include <iostream>
using namespace std;
int *pancakes = new int[10];
int *pancakes_one = new int[10];
int *pancakes_two = new int[10];
class Person
{
public:
void sort_pancakes(int*);
void num_pancakes(int*);
void output_pancakes(int*);
private:
int number;
int pancakes;
int panckes_one;
int pancakes_two;
};
void Person::num_pancakes(int *pancakes)
{
for(int i=0; i < 10; i++)
{
cout << "Enter number of pancakes eaten: ";
cin >> pancakes[i];
}
}
void Person::sort_pancakes(int *pancakes_one)
{
for(int i=0; i<10;i++)
{
for(int j=0;j<i;j++)
{
if(pancakes_one[i]<pancakes_one[j])
{
int temp = pancakes_one[i];
pancakes_one[i] = pancakes_one[j];
pancakes_one[j] = temp;
}
}
}
}
void output_pancakes(int *pancakes_two)
{
for(int k=0; k<10;k++)
{
cout << "Person " << k << " ate " << pancakes_two[k] << " pancakes." << endl;
}
}
int main()
{
Person pancake;
pancake.num_pancakes(pancakes);
pancake.sort_pancakes(pancakes_one);
pancake.output_pancakes(pancakes_two);
int test;
cin >> test;

return 0;
}
[/code]

I am however getting a linker error:
LINK : C:\Users\phil\Desktop\size\Debug\size.exe not found or not built by the last incremental link; performing full link
1>size.obj : error LNK2019: unresolved external symbol "public: void __thiscall Person::output_pancakes(int *)" (?output_pancakes@Person@@QAEXPAH@Z) referenced in function _main
1>C:\Users\phil\Desktop\size\Debug\size.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have traced the problem to the "pancake.output_pancakes(pancakes_two);"
line of code.
something is wrong in my member function, I think??
0

Share this post


Link to post
Share on other sites
I have decided to finish my sorting problem,I have made some changes to my code,I am using two arrays to store pancakes and people,I then sort the values inputted,by using a bubble sort,I then output the values.the hard part is getting the values to parallel each other. In other words I only want the pancakes to be sorted but I want the persons to be related to each pancake value.Here is my code:
[code]
#include <iostream>
using namespace std;
int pancake[11];
int person[11];
int main()
{
for(int i=1;i<=10;i++)
{
cout << "Enter number of pancakes eaten: ";
cin >> pancake[i];
person[i]=i;
}
for(int i=1; i<=10; i++)
{
for(int j=1;j<=i;j++)
{
if(pancake[i]>pancake[j])
{
int temp = pancake[i];
pancake[i]=pancake[j];
pancake[j]=temp;
}
}
}
for(int i=1; i<=10; i++)
{
for(int j=1;j<=i;j++)
{
if(person[i]>person[j])
{
int temp = person[i];
person[i]=person[j];
person[j]=temp;
}
}
}
for(int i=1; i<=10;i++)
{
cout << "Person " << person[i] << " ate " << pancake[i] << " pancakes." << endl;
}
int test;
cin >> test;
return 0;
}
[/code]

I really want to finish this project.
0

Share this post


Link to post
Share on other sites
If you have two arrays you can't sort them independently. That will give a sorted pancake array and a sorted person array without preserving the relation between them. If you want to sort both of them by the number of pancakes then sort the pancakes, but every time you swap two elements of the pancake array swap the corresponding members of the person array.
1

Share this post


Link to post
Share on other sites
I have decided to rework my code,but now it won't sort my array and output it.
[code]
#include <iostream>
#include <stdlib.h>
using namespace std;
int arr[10]={0};
void bubbleSort(int arr[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
int main()
{
for(int i=0;i<10;i++)
{
cout << "Enter number of panckes eaten: ";
cin >> arr[i];
}
int n=10;

for(int j=0;j<10;j++)
{
void bubbleSort(int arr[],int n);
}

for(int i=0; i<10;i++)
{
cout << "Person " << i << " ate " << arr[i] << " pancakes." << endl;
}

system("pause");
return 0;
}
[/code]

i did this to better understand functions
0

Share this post


Link to post
Share on other sites
To call a function, you have to pass in parameters.

"void bubbleSort(int arr[],int n);" is used to [i]create [/i]a function. You never actually [i]use [/i]the function.

Your "bubbleSort()" function won't know what array to use, and won't know what 'n' is set to, until you actually pass it parameters like this:
[code]bubbleSort(myArray, 10);[/code]

Further, you don't need to call the function 10 times (just once in your specific example), so it doesn't need to be in the for() loop.

[hr]

When a function defines parameters, those parameters are variables. When you pass in variables to a function call, you are saying "[i]Set the function's parameters are to the value of the variables I'm passing in to it[/i]".

The parameters do not need to have the same name as the variables being passed in. (You are passing in the [i]data[/i] your variables hold, not the variables themselves).

Likewise, when a function returns a value, it's returning the [i]data[/i] that you can then assign to a different variable.

Example: (this is important to understand)
[code]int myFunction(int parameterA, int parameterB)
{
return (parameterA + parameterB);
}

int main()
{
int result1 = myFunction(15, 20); //Passing in data from temporary variables without names that are set to 15 and 20.
std::cout << "result1: " << result1 << std::endl;

int var1 = 1004;
int var2 = 230;

int result2 = myFunction(var1, var2); //Passing in data held in variables. (The variables aren't passed in, just *copies* of the data they hold)
std::cout << "result2: " << result2 << std::endl;

int result3 = myFunction(var2, 23000); //Passing in data from temporary and non-temporary variables.
std::cout << "result3: " << result3 << std::endl;

system("pause");
return 0;
}[/code]

A number like "45" just floating in your code can be thought as (but isn't exactly) an unnamed temporary variable with the value of 45. These are called 'literals'.

When you go:
[code]int myVar = 45;[/code]
You are saying, "[i]*copy* the data '45' into the variable 'myVar'[/i]".

When you go:
[code]int myOtherVar = myVar;[/code]
You are saying: "[i]*copy* the data held in myVar into the variable 'myOtherVar'[/i]".

If you have the code:
[code]void myFunction(int param1, int param2);

int main()
{
int myVar = 45; //Holds data equal to the value of '45'.
int myOtherVar = myVar; //Also holds data equal to the value of '45'.

myFunction(myVar, myOtherVar);

return 0;
}[/code]

On the line that goes:
[code]myFunction(myVar, myOtherVar);[/code]
You are saying "[i]*copy* that data of 'myVar' into myFunction's variable 'param1'. Also, *copy* that data held in 'myOtherVar' into the variable 'param2'."[/i]

It is exactly the same as going like this:
[code]int param1 = myVar;
int param2 = myOtherVar;[/code]
Except that 'param1' and 'param2' can only be used inside myFunction().

You are *copying* the data, [i]not[/i] the variables themselves. (Variables hold data. It's the data you are passing around, not the containers holding them). Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
I finallly figured out my sorting program.thanks for all the help.

[mod edit] merged with your original thread Edited by Hodgman
0

Share this post


Link to post
Share on other sites
[quote name='Lloydg' timestamp='1349315681' post='4986627']
ok, what was it? how did you solve it?
[/quote]
His original thread can be [url="http://www.gamedev.net/topic/631676-c-sorting-problem/page__p__4982724#entry4982724"]found here[/url]. I'm not sure why he made this thread... perhaps a mistake.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0