Here is what I have so far.

``````#include <iostream.h>

int main() {

int i, total;
int myValue;

myValue = 53000;
myValue = 5;
myValue = 1;

myValue = 89000;
myValue = 3;
myValue = 2;

myValue = 93000;
myValue = 3;
myValue = 3;

for (i = 0; i < 3; i++)
{
total[i] = (myValue[i]/1000) + (myValue[i] - myValue[i]);
}

cout << total <<endl;
cout << total <<endl;
cout << total <<endl;

return 0;

}
``````

I forgot a variable. I need to add a name in each of my arrays. But for some reason im getting an error saying I can't use a CHAR and a INT??? How can I do

``````myValue = Greg;
myValue = 53,000;
myValue = 5;
myValue   = 1;
``````

When i print the result, I need the persons name to show up with the number. I been tryin to figure it out for myself for over an hour now

Are you so impatient as to insult everyone on this board by starting a new thread when nobody answers your followup question on the other thread? It hasn't been that long.

>I need to add a name in each of my arrays.
You're thinking about the problem wrong. You …

>I need the shortest first
The shortest name? What if they're all the same length? There's a function that you can use in the string.h header called strcmp, it returns <0, 0, or >0 depending on the relation of the first and second arguments. Working out how to print in …

Greetings,

If I understand correctly, you want to find which of the totals are shortest. Like from minimum to maximum, e.g. A is less than B so B is greater; B is greater than C so C is less than B.

Maximum to minimum example:
B [is greater …

>I can't figure out a code to sort the total in accending order
You aren't trying very hard then, are you? Sorting is a very well known classical problem in computer science. If you search for anything even related to sorting on google you'll get millions of hits.

I agree with Narue. It may help if you search for bubble sorting. It's a famous algorithm for sorting all types of data.

- Stack Overflow

## All 17 Replies

Are you so impatient as to insult everyone on this board by starting a new thread when nobody answers your followup question on the other thread? It hasn't been that long.

>I need to add a name in each of my arrays.
You're thinking about the problem wrong. You need to attach a name to the use of each array.

>myValue = Greg;
>myValue = 53,000;
This will never work in a language like C++. How about something more like this:

``````#include <string.h>

...

char name;
int myValue;

...

strcpy ( name, "Greg" );
myValue = 53,000;
myValue = 5;
myValue = 1;``````

My apologies on this. Now this is what i have for my output
cout << name << total <<endl;
cout << name <<total <<endl;
cout << name <<total <<endl;

how could i possible make this in accending order? I need the shortest first..Better yet how can it go into a priority queue? We just started on queue I understand the concept of it, but can't really put my finger on how it works.

>I need the shortest first
The shortest name? What if they're all the same length? There's a function that you can use in the string.h header called strcmp, it returns <0, 0, or >0 depending on the relation of the first and second arguments. Working out how to print in ascending order from there is simple.

No not the shortest name. The shortets result from total execution. How could I get it into a priority queue.

Greetings,

If I understand correctly, you want to find which of the totals are shortest. Like from minimum to maximum, e.g. A is less than B so B is greater; B is greater than C so C is less than B.

Maximum to minimum example:
B [is greater than A and C]
C [is greater than A but less than B]
A [is the least of them all]

- Stack Overflow

Greetings,

If I understand correctly, you want to find which of the totals are shortest. Like from minimum to maximum, e.g. A is less than B so B is greater; B is greater than C so C is less than B.

Maximum to minimum example:
B [is greater than A and C]
C [is greater than A but less than B]
A [is the least of them all]

- Stack Overflow

Thats correct, but im having trouble just sorting them. I can't figure out a code to sort the total in accending order

>I can't figure out a code to sort the total in accending order
You aren't trying very hard then, are you? Sorting is a very well known classical problem in computer science. If you search for anything even related to sorting on google you'll get millions of hits.

I agree with Narue. It may help if you search for bubble sorting. It's a famous algorithm for sorting all types of data.

- Stack Overflow

Ok, I looked up sorting and found qsort. I also been playing around
here is what i have

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct RECORD {
char name;
int num1, num2, num3;
};

int compareProduct (RECORD *rec1, RECORD *rec2)
{ return (rec1->num1 * rec1->num2) - (rec2->num1 * rec2->num2);
}

void main(void)
{
RECORD values =
{ { "Brian Devaux   ",53000, 5, 1 },
{ "Amanda Trapp   ",89000, 3, 2 },
{ "Baclan Nguyen  ",93000, 3, 3 },
{ "Sarah Gilley   ",17000, 1, 4 },
{ "Warren Rexroad ",72000, 7, 5 },
{ "Jorge Gonzales ",65000, 2, 6 },
{ "Paula Hung     ",34000, 3, 7 },
{ "Lou Mason      ",21000, 6, 8 },
{ "Steve Chu      ",42000, 4, 9 },
{ "Dave Lightfoot ",63000, 3, 10},
{ "Joanna Brown   ",33000, 2, 11},

};
int i;

/* Show the original order of the records */
for (i=0; i < 11; i++)
printf ("%10s %3d %3d %3d\n",
values[i].name,values[i].num1,values[i].num2, values[i].num3);

/* now sort them based on the max product of the nums */
qsort (values,11,sizeof(RECORD),
(int (*)(const void *,const void *)) compareProduct);

/* Show the records after they're sorted */
printf ("\n\n");
for (i=0; i < 11; i++)
printf ("%10s %3d %3d %3d %4d\n",
values[i].name,values[i].num1,values[i].num2,values[i].num3,
(values[i].num1/1000) + (values[i].num2 - values [i].num3));

}
``````

now im missing something, for some odd reason my numbers start to sort, then they dont. any help?

Greetings,

I just noticed that your comparing command compareProducts() has a slight issue. Since we can only sort one number at a time, usually, we will have to resort to checking if the two rec[n]'s differ.

``return (rec1->num2 - rec2->num2); // 1 up``

You may notice that num2 actually sorts. You can reverse the process if you want to descend say:

``return (rec2->num2 - rec1->num2); // 7 down``

Hope this helps,
- Stack Overflow

Greetings,

As you may want to sort the last number calculation try the following code in your compareProducts() function:

``return ( ( (rec1->num1/1000) + (rec1->num2 - rec1->num3) ) - ( (rec2->num1/1000) + (rec2->num2 - rec2->num3) ) );``

What this does is it takes your current calculation of the one value and subtracts from the other, literaly knowing which one to sort. I looked at the new sort lineup, and it seems to work for me. I hope this same applies for you.

- Stack Overflow

Here is my original Problem

An airline company uses the formula shown below to determine the priority of passengers on the waint list for overbooked flights

Formula (A/100 + B - C

Given the file of oberbooked customers shown in the table (the table is interpreted above). Wirite a program that reads the file and dtermines the customer priority number. The program then builds a priority queue using hte priority number and prints a list of waiting customers in priority sequence

how the table looks in the book, and the data is already used in my program.

NAME MILEAGE YEARS SEQUENCE

Alright,

Seems understandable. A priority queues is a container adaptor which allows use of any sequential container with a random access iterator to maintain a sorted collection of items. You can specify a comparator which is used to sort the items.

They are a bit complex, though if they instructor taught on them I would say it wouldn't be a problem to use one. If you want to further look at how Priority Queue's work, you can check out the following links:

Hope this helps,
- Stack Overflow

Anyone wanna get me started on how to do this. LOL ...........Struggling for survival here :)

>It may help if you search for bubble sorting. It's a famous algorithm for sorting all types of data.
Famous for being the canonical example of a bad algorithm, sure. Please don't teach it because there are no reasons to favor bubble sort over any of the other quadratic sorts.

>LOL ...........
:rolleyes:

>Struggling for survival here
It looks more like you're waiting for us to do your research for you and hand you everything you need to do the project. I offer you six letters that will change your life: G.O.O.G.L.E.

How about I have....Im new...But thanks for the help. There are sooo many implementations of stuff out there

I see...

>Im new...
That's no excuse for being lazy.

>There are sooo many implementations of stuff out there
So go grab one and play with it! Is tinkering such a foreign concept to you people these days? You know, when I started programming we had to figure it all out on our own. The web wasn't the treasure trove of solved problems that it is now. In fact, I'm shocked and amazed that anyone needs to ask questions around here because it's so EASY to get started with a search. Hell, most of the time it's not even a start that you get, but the entire solution!

Anyway, here's another full implementation since you seem incapable of finding one on your own. Insertion sort is one of the more useful quadratic sorts, and it's a wonderful introduction into sorting without resorting to bubble sort:

``````#include <stdio.h>
#include <stdlib.h>

void insertion_sort ( int a[], int left, int right )
{
int i, j;

for ( i = 1; i < right; i++ ) {
int save = a[i];

for ( j = i - 1; j >= left && a[j] > save; j-- )
a[j + 1] = a[j];

a[j + 1] = save;
}
}

int main ( void )
{
int a;
int i;

for ( i = 0; i < 15; i++ )
a[i] = (int)( 1.0 * rand() / RAND_MAX * 100 );

for ( i = 0; i < 15; i++ )
printf ( "%4d", a[i] );
printf ( "\n" );

insertion_sort ( a, 0, 15 );

for ( i = 0; i < 15; i++ )
printf ( "%4d", a[i] );
printf ( "\n" );

return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.