New to C and haven't had any problems until now -
I can seem to get straight with passing arrays between functions.

Here is what I need to do:

Write a program that declares 3 single-dimension arrays named bookPrice, numBooks and totAmt. Each array should be declared in main() and capable of storing 6 values. Make up numbers for bookPrice and numBooks. 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().

What I have is totaling all of the amounts into one instead of a total for each array.

My output is:

3 Books at $ 9.50 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
6 Books at $10.99 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
2 Books at $ 7.50 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
4 Books at $ 6.99 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
5 Books at $ 8.25 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
3 Books at $19.95 each = $-9255963134931783100000000000000000000000000000000000
0000000000.00
Press any key to continue . . .

Here is the code I have:

#include "stdafx.h"
#define NUM 6
double calc(double[NUM], double[NUM]);

int _tmain(int argc, _TCHAR* argv[])
{
	int i;

	double bookPrice[NUM] = {9.5, 10.99, 7.5, 6.99, 8.25, 19.95};
	double numBooks[NUM] = {3, 6, 2, 4, 5, 3};
	double totAmt[NUM];

	
	calc(bookPrice, numBooks);
	for (i = 0; i < NUM; i++)
		
			printf("%2.0f Books at $%5.2f each = $%5.2f\n", numBooks[i], bookPrice[i], totAmt[i]);
	
	return 0;
}
double calc(double price[NUM], double num[NUM])
{
	double total[6];
	int i;
	for (i = 0; i < NUM; i++)
	total[i]= price[i] * num[i];
	return(total[i]);
}

I have been working on this for days and am really have a mental block..Any help would be appreciated

Recommended Answers

All 2 Replies

You never assign anything to the variable totAmt.

The solution you are looking for is stated in the actualy question text you have posted

Your program should pass these 3 arrays to a function named calc()

You are only passing 2 of those 3 arrays. Pass the 3rd one and you can fill it in. You do not need the total temporary variable in calc.

Ugh...thanks I knew it was something stupid, have it working now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.