![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 4
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
- 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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 4
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() ![]() |
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:
![]() |
|
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
~ Code Tags added in OPs post.
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Feb 2006
Location: Hungary
Posts: 4
Rep Power: 0
![]() |
Hi!
You have to take zero myTotal after printed the count of sales of one car. Morzel |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
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; One reason they are not adding up the right amount is the 0 in this code: myTotal=myTotal+sales[0][myCol] 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}
}; |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
Quote:
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. |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: May 2006
Posts: 4
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|