![]() |
|
#1
|
|||
|
|||
need help with passing 3 arrays into a functionI 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:
|
|
#2
|
||||
|
||||
|
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. __________________
Age is unimportant -- except in cheese |
|
#3
|
|||
|
|||
|
My output is:
CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
|
CPP / C++ / C Code:
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
|
|||
|
|||
|
Never mind!! I changed this code:
Code:
Code:
Thanks for everyone's help, esp. to Aaron and Waltp. You guys definitely know your programming and should be promoted |
|
#6
|
||||
|
||||
|
Quote:
Members get promoted automatically by their REPUTATION, so if they helped you, please use the REPUTATION feature to pass them some well deserved points. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#7
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
|
|
#9
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
|
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help on passing in arrays in functions? | nusstu | C Programming Language | 10 | 02-Apr-2004 10:42 |
| Another problem dealing with main() and driver function | tommy69 | C Programming Language | 4 | 20-Mar-2004 19:46 |
| Passing Pointers To Pointers in Functions | elumira | C Programming Language | 8 | 05-Mar-2004 21:23 |
| Calling functions within a function | spudtheimpaler | C Programming Language | 5 | 02-Mar-2004 08:02 |
| i/o: set value x (long) before it is used by a function | gmn | C Programming Language | 1 | 18-Nov-2003 01:12 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The