943,516 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5030
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 14th, 2004
1

I would kill for this, just a little bit

Expand Post »
Here is what I have so far.

#include <iostream.h>

int main() {

int i, total[3];
int myValue[3][3];

myValue[0][0] = 53000;
myValue[0][1] = 5;
myValue[0][2] = 1;


myValue[1][0] = 89000;
myValue[1][1] = 3;
myValue[1][2] = 2;

myValue[2][0] = 93000;
myValue[2][1] = 3;
myValue[2][2] = 3;

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

cout << total[0] <<endl;
cout << total[1] <<endl;
cout << total[2] <<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[0][0] = Greg;
myValue[0][1] = 53,000;
myValue[0][2] = 5;
myValue [0] [3] = 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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
HollywoodTimms is offline Offline
13 posts
since Oct 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

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[0][0] = Greg;
>myValue[0][1] = 53,000;
This will never work in a language like C++. How about something more like this:
  1. #include <string.h>
  2.  
  3. ...
  4.  
  5. char name[3][20];
  6. int myValue[3][3];
  7.  
  8. ...
  9.  
  10. strcpy ( name[0], "Greg" );
  11. myValue[0][0] = 53,000;
  12. myValue[0][1] = 5;
  13. myValue[0][2] = 1;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

My apologies on this. Now this is what i have for my output
cout << name[0] << total[0] <<endl;
cout << name[1] <<total[1] <<endl;
cout << name[2] <<total[2] <<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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
HollywoodTimms is offline Offline
13 posts
since Oct 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

No not the shortest name. The shortets result from total[i] execution. How could I get it into a priority queue.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
HollywoodTimms is offline Offline
13 posts
since Oct 2004
Oct 14th, 2004
1

Re: I would kill for this, just a little bit

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

Quote originally posted by 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[i] in accending order
Reputation Points: 11
Solved Threads: 0
Newbie Poster
HollywoodTimms is offline Offline
13 posts
since Oct 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

>I can't figure out a code to sort the total[i] 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 14th, 2004
0

Re: I would kill for this, just a little bit

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[20];
int num1, num2, num3;
};


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


void main(void)
{
RECORD values[11] =
{ { "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?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
HollywoodTimms is offline Offline
13 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Im completely lost on this one
Next Thread in C Forum Timeline: pls help me in prgm of stack





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC