GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Apr-2004, 23:59
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

need help with passing 3 arrays into a function


I have another program and boy, this is a tough one as well. I think I am slowly getting better and this is slow, but surely making sense the more I program. Sorry to keep bugging everyone.

Here is what I need to do:

I need to write a program that declares 3 single-dimensional arrays named bookPrice, numBooks, and totAmt. Each array should be declared in main90 and capable of storing 6 values. Make up numbers for price and quantity. Your program should pass these 3 arrays to a function named calc(), which should calculate the elements in the totAmt array as the product of the corresponding elements in bookPrice and numBooks. After calc() has put values into the totAmt array, the values in the array should be displayed from within main().

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

float calc( float bookPrice[], int numBooks [], float totAmt[],int numEls);  /*function prototype*/
                                     
int main()
{
     int a;
     
     float bookPrice[6]={4.95,6.50,8.75,12.25,16.60,20.50};
     int numBooks[6]={4,6,9,12,16,19};
     float totAmt[6];
	
	float calc();

	for (a=0;a<=6;++a)
		printf("\nThe total prices for each book is:  %.2f\n",  totAmt);

     getch();

     return 0;

}
float calc(float bookPrice[], int numBooks[], float totAmt[], int numEls) /*function declaration*/
{
     int a;

	
	 for (a=1;a<numEls;++a)
	
	   totAmt[a]=numBooks[a]*bookPrice[a];
	 
	 return (totAmt[a]);

}
  #2  
Old 05-Apr-2004, 02:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Well, you told us what you need to do
You showed us what you wrote
You did not tell us what is wrong.

Please give us all the information we need to understand the problem you're having.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 05-Apr-2004, 06:19
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
My output is:

CPP / C++ / C Code:

The total prices for each book is:  0.00

The total prices for each book is:  0.00

The total prices for each book is:  0.00

The total prices for each book is:  0.00

The total prices for each book is:  0.00

The total prices for each book is:  0.00

The total prices for each book is:  0.00
The output should be the product of multiplying each element of the numBooks array by each element of the bookPrice array to get values into the totAmt array and then display these new values of the totAmt array.
  #4  
Old 05-Apr-2004, 07:09
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
CPP / C++ / C Code:

#include <stdio.h>
#include <conio.h>

float calc( float bookPrice[], int numBooks [], float totAmt[],int);  /*function prototype*/
                                     
int main()
{
     int a;
     
     float bookPrice[6]={4.95, 6.50, 8.75, 12.25, 16.60, 20.50};
     int numBooks[6]={4,6,9,12,16,19};
     float totAmt[6];
	
	 calc(bookPrice,numBooks,totAmt,6); 

	for (a=0;a<=6;++a)
		printf("\nThe total prices for each book is:  %.2f\n",  totAmt);

     getch();

     return 0;

}
float calc (bookPrice[],numBooks[],totAmt[],int numEls) /*function declaration*/
{
     int a;

 
	 for (a=1;a<numEls;++a)
	
	 totAmt[a] = bookPrice[a]*numBooks[a]; 
	   
	 
	 return (totAmt[a]);

}
Output is:

The total prices for each book is: -107374176.00

The total prices for each book is: 39.00

The total prices for each book is: 78.75

The total prices for each book is: 147.00

The total prices for each book is: 265.60

The total prices for each book is: 389.50


Now all I am trying to figure out is why the first print otu is wrong.
  #5  
Old 05-Apr-2004, 07:26
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Never mind!! I changed this code:
Code:
for (a=1;a<numEls;++a)
to
Code:
for (a=0;a<numEls;++a)

Thanks for everyone's help, esp. to Aaron and Waltp. You guys definitely know your programming and should be promoted
  #6  
Old 05-Apr-2004, 08:25
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by tommy69
...You guys definitely know your programming and should be promoted

Members get promoted automatically by their REPUTATION, so if they helped you, please use the REPUTATION feature to pass them some well deserved points.
  #7  
Old 05-Apr-2004, 13:06
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Okay, tommy... well here's one thing I noticed. At the end of your calc function, your return value is totAmt[a]. The problem with this is that you're only returning the value stored in whatever element is in totAmt[a]. There's another problem. You don't want to return just one piece of the array, you want to return the whole thing. The deal with that is that you don't need to! Arrays are always passed byreference. This means that you do not have to have a return value in your function when you want to return an array. You can simply make your changes to the array within the function, and those changes will appear when you get back into main(). So, again, you can just let the function return void. Whatever changes you make to the array inside the function will also happen outside the function, so you do NOT need to return it.

For the output, your printf statement is trying to display the entire array at once, whereas you only want to display one element at a time. You have it in a loop that uses "a" as a counter, but that's all "a" is doing. Put "a" in the index for totAmt in your printf, and you'll be in business. Also, as a rule of thumb, usually you want to use a++ rather than ++a when you do increment operations, unless you specifically want ++a. You know what ++a does, right?

*edit* I have 200 posts and a spectacular aura about me
__________________
-Aaron
  #8  
Old 05-Apr-2004, 13:17
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
This is the code that I handed in to the teacher. It's not much different thatn the one I posted earlier. For the printf statement, I did sue the totAmt[a]. I found this problem to be easier and maybe it's because I am slowly learning. Plus this problem is easy, esp for you guys.

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

float calc( float bookPrice[], int numBooks [], float totAmt[], int);  /*function prototype*/
                                     
int main()
{
     int a=0;
     

     float bookPrice[6]={4.95, 6.50, 8.75, 12.25, 16.60, 20.50};
     int numBooks[6]={4,6,9,12,16,19};
     float totAmt[6];
	
	 calc(bookPrice,numBooks,totAmt,6); 

	for (a=0;a<=5;a++)
		printf("\nThe total prices for each book is:  %.2f\n",  totAmt[a]);

     getch();

     return 0;

}
float calc (float bookPrice[],int numBooks[], float totAmt[],int numEls) /*function declaration*/
{
     int a;

	 for (a=0;a<numEls;a++)
	
	 totAmt[a] = bookPrice[a]*numBooks[a]; 
	  	 
	 return (totAmt[a]);

}
  #9  
Old 05-Apr-2004, 13:19
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Were you forced to hand it it? I wouldn't hand it in unless it worked, even if that meant turning it in late.
__________________
-Aaron
  #10  
Old 05-Apr-2004, 13:27
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Quote:
Originally Posted by aaroncohn
Were you forced to hand it it? I wouldn't hand it in unless it worked, even if that meant turning it in late.
Pretty much so because this is the only teacher that I have ever known that we CAN"T turn it in late. It's due on the due date and if you try to turn it in late, you get a zero. Not sure why she does this, but there isnt anything I can do about it. I hope to get a decent grade. I didnt get a very good grade on one of my programs because I was stuck on a problem and I just turned it in anyways. I hate to keep asking people here about every problem I have.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on passing in arrays in functions? nusstu C Programming Language 10 02-Apr-2004 11:42
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 20:46
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 22:23
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 09:02
i/o: set value x (long) before it is used by a function gmn C Programming Language 1 18-Nov-2003 02:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.