Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 8th, 2006, 11:25 PM   #1
MasterYoda
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 MasterYoda is on a distinguished road
anybody who wish to help me..

good day..

i am new in C programming..i wish i could learn somthing in C.recently i try to do exercise on multidimentional array but i couldn't find the solution so anybody who can help me?


i try to do it but i cannot get the entire output that suppost to be displayed.

here is the question:

An automobile company has five dealers, each selling four types of cars. The number of cars sold by model and dealers is shown below.

Dealer | Model 1 | Model 2 | Model 3 | Model 4
1 20 3 40 80
2 6 23 0 28
3 11 83 48 61
4 12 1 4 6
5 19 21 28 50

Find and print the total cars sold by each dealer and the total sale of each model. (Use For Loop)

Sample output:

The total sale of Dealer 1 is 170
The total sale of Dealer 2 is 57
The total sale of Dealer 3 is 203
The total sale of Dealer 4 is 23
The total sale of Dealer 5 is 118

The total sale of Model 1 is 68
The total sale of Model 2 is 158
The total sale of Model 3 is 120
The total sale of Model 4 is 225

--------------------------------------------------------------------

#include <stdio.h>

void main(void)

{

int sales[5][4] = {20,30,40,80,6,23,0,28,11,83,48,61,12,1,4,6,19,21,28,50}
int myCol;
int i;
int mytotal=0


for (i= 1; i<= 5; i+=1)

  {

    for (myCol=1;myCol<=3;myCol+=1)

    myTotal=myTotal+sales[0][myCol] 

    printf("\n");

    printf("The total sale of %d is %d",i,myTotal);


  }

}

Last edited by Mjordan2nd; May 9th, 2006 at 12:29 AM.
MasterYoda is offline   Reply With Quote
Old May 8th, 2006, 11:29 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Please repost your code in [ code ] [/ code ] tags (without the spaces). This allows others to more clearly understand what has been written.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; May 8th, 2006 at 11:39 PM.
Jessehk is offline   Reply With Quote
Old May 8th, 2006, 11:45 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
- Is it just me, or is your code missing semi-colons? Or are those not always needed in C?
- Lines like myTotal=myTotal+sales[0][myCol] can by simplified to myTotal += sales[0][myCol].
- Your two printf statements could have been combined in to one (put "\n" at the beginning of the second printf.
- In your for loop, I think you may want to double check myCol's limit.
- "mytotal" keeps incrementing throughout the entire program, you don't exactly want that to happen.
- If you want to have two chunks of outputs, you're going to need two chunks of code.

I don't want to give away the main conceptual logic error to you. A great part about programming is realising what you've done inproperly/incorrectly for yourself.

Good luck.
Sane is offline   Reply With Quote
Old May 8th, 2006, 11:47 PM   #4
MasterYoda
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 MasterYoda is on a distinguished road
sane,

my department had asked to to learn C as they will give me a project using C..

i've fix that error but the truth is i can't get the real output as below:

The total sale of Dealer 1 is 170
The total sale of Dealer 2 is 57
The total sale of Dealer 3 is 203
The total sale of Dealer 4 is 23
The total sale of Dealer 5 is 118

The total sale of Model 1 is 68
The total sale of Model 2 is 158
The total sale of Model 3 is 120
The total sale of Model 4 is 225

according to my coding,the output that i get is:

The total sale of Dealer 1 is 170
The total sale of Dealer 2 is 340
The total sale of Dealer 3 is 510
The total sale of Dealer 4 is 680
The total sale of Dealer 5 is 850

the main problem is the value 170,340,510,6,80,850

Last edited by MasterYoda; May 9th, 2006 at 12:04 AM.
MasterYoda is offline   Reply With Quote
Old May 8th, 2006, 11:55 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Are you talking to Jessehk, or me? Jessehk's telling you to place the text [ code ] (without the spaces) before your code starts, and [ /code ] (without the spaces) after your code ends. Whenever you post your code in the forums, it is customary to do this to format it to become more readable...

For example
#include <stdio.h>
int main()
{
    return 0;
}

Is easier to understand then:

#include <stdio.h>
int main()
{
return 0;
}



By the way... out of shear curiosity. What is a 26 year old doing learning C? Again... just because this is the kind of thing to perk up my ears. <_<



EDIT

Yes, I realise that Yoda. However, I'm telling you that I won't show you what's wrong logically with your code. That's for you to figure out by yourself. I've given vital hints in my earlier post as to what's required. Mainly here...

Quote:
Originally Posted by Sane
- If you want to have two chunks of outputs, you're going to need two chunks of code.
It'll be much better for you to take what I've told you and try once more. I doubt anyone else here will take you by the hand and give you the code required to complete this assignment. If there's someone like that here, then I guess they're a much better person then I.
Sane is offline   Reply With Quote
Old May 9th, 2006, 12:30 AM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
~ Code Tags added in OPs post.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old May 9th, 2006, 12:49 AM   #7
Morzel
Newbie
 
Join Date: Feb 2006
Location: Hungary
Posts: 4
Rep Power: 0 Morzel is on a distinguished road
Hi!

You have to take zero myTotal after printed the count of sales of one car.

Morzel
Morzel is offline   Reply With Quote
Old May 9th, 2006, 12:56 AM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
I have to disagree with you here Sane, if someone has registered and posted to the forum and shown that they have tried to get the program to work (even without code tags), helping them with some simple logic doesn't seem like to much to ask, after they haven't understood some hints.

Yoda:
The reason your numbers are increasing each time is that your are not resetting the value of the myTotal variable each time round the loop. Just stick
myTotal=0;
at the top of the inside of the for loop that is incrementing i.

One reason they are not adding up the right amount is the 0 in this code:
myTotal=myTotal+sales[0][myCol]
That should be i.

As Sane said, you need to change the limits of your for loops. In C, arrays start from 0, so your rows should go from 0 to 4 (inclusive) and you columns should go from 0 to 3.

For preference, I would also change your array initialiser to
int sales[5][4] = 
{
  {20,30,40,80},
  {6,23,0,28},
  {11,83,48,61},
  {12,1,4,6},
  {19,21,28,50}
};
It just makes it easier to see where the columns are.
The Dark is offline   Reply With Quote
Old May 9th, 2006, 1:07 AM   #9
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
Quote:
By the way... out of shear curiosity. What is a 26 year old doing learning C? Again... just because this is the kind of thing to perk up my ears. <_<
...???...

i turned 26 friday.

but you're right, if you go back to school at a later age, they don't teach you C, they teach you pascal and fortran.

for your first semester project they just give you a cardboard box full of vacuum tubes and transistors and tell you to create an OS.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; May 9th, 2006 at 1:23 AM.
bl00dninja is offline   Reply With Quote
Old May 9th, 2006, 2:51 AM   #10
MasterYoda
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 MasterYoda is on a distinguished road
Dark,

myTotal=myTotal+sales[0][myCol]

if i change the value 0 to i , i won't get the value as below

The total sale of Dealer 1 is 170
The total sale of Dealer 2 is 57
The total sale of Dealer 3 is 203
The total sale of Dealer 4 is 23
The total sale of Dealer 5 is 118

what make me feel headache is to get that 170,57,203,23,118
MasterYoda is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC