?
Solved

Arrays Pointers and Functions II

Posted on 2004-10-19
4
Medium Priority
?
244 Views
Last Modified: 2010-04-01
I am trying to learn about pointers and arrays. In this program I playing with several concepts, however, I dont have the funtion right (I only included 1 of several). What am I doing wrong? Thanks much, Annie
#include <iostream>
#include "inputarray.cpp"
           /*struct input{double numCostPerPart; int numPublished;
            //double numSalCost; int numSold; double numRoyalPercent;
           double numHolePrice;};*/
#include "outputarray.cpp"
          /*struct output
          {double numTotalCost; double numSalValue; double numGrossProfit;
           double numRoyalty; double numProfit;};*/

using namespace std;
const int SIZE =3;  
void makeCalculations(input ainput[], int nSize);

int main()
{
      int n;
      
      input ainput[SIZE];    //declare array input
      output aoutput[SIZE];  //decalre array output
      
      
      for(n=0; n<SIZE; n++)  //loops  array
      {
            cout<<endl;

            cout<<"Enter cost of parts: ";
            cin>>ainput[n].numCostPerPart;
            cout<<"Enter number of parts published: ";
            cin>>ainput[n].numPublished;
            cout<<"Enter salvation cost of parts: ";   //inputs
            cin>>ainput[n].numSalCost;
            cout<<"Enter number of parts sold: ";
            cin>>ainput[n].numSold;
            cout<<"Enter Royalty Percentage: ";
            cin>>ainput[n].numRoyalPercent;
            cout<<"Enter wholesale cost of parts: ";
            cin>>ainput[n].numHolePrice;
      }
      cout << endl;
      makeCalculations(ainput, 1);
      
      for( n=0; n<SIZE; n++)  //loops  output array
      
      {
      cout << "Total Cost"<<aoutput[n].numTotalCost<<endl;
      cout <<"Salvation Value" <<aoutput[n].numSalValue<<endl;   // outputs
      cout <<"Gross Profit" << aoutput[n].numGrossProfit<<endl;
      cout <<"Royalty" << aoutput[n].numRoyalty<<endl;
      cout <<"Profit" << aoutput[n].numProfit<<endl;
      }
      system("pause");
      return EXIT_SUCCESS;
}

 
  void makeCalculations(input ainput[], int nSize)
  {

      double totalCost = 0;
     
        for (int n = 0; n < nSize; ++n)
        {
          totalCost += ainput[n].numCostPerPart;
             
         }
            ainput[n].numCostPerPart* totalCost;
  }
0
Comment
Question by:bananaamy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12354641
Hi bananaamy,

I found many errors in the code. It seems to be written in Dev-C++ (just a thought). In the function why is the line 'ainput[n].numCostPerPart* totalCost;' out of the for loop? I think it should be calculated as a part of the loop. The error comes because the 'n' used in the line is finishes as soon as the loop finished. So, I take it inside the loop.

Please explain the problem that you are facing. Are you getting wrong results??

Cheers!
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12355381
Your makeCalculations doesn't store totalCost anywhere.

Try changing...

    ainput[n].numCostPerPart* totalCost

...to...

    ainput[n].numCostPerPart = totalCost;

Note also that you are passing size as 1 to this fnction in the calling code, which means that it will only operate on the first index of the array.
0
 

Author Comment

by:bananaamy
ID: 12356499
I am trying to learn this languge and have been experimenting with it for about 4 weeks. What I am tring to do is to see how  several things work together(pointers,arrays and functions). . I want to enter in data into an "input array" a specific amount of times,  I then want to pass the value to a function, preform several calculations, and pass the value of the output of that function to the "output array". I want the output array to display the answers to my calculations. What  I cant figure out is  how to make my functions work with pointers.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 2000 total points
ID: 12358432
Start by passing your output array as a parameter to makeCalculations. When you pass an array as a parameter to a function, it is "passed by reference", which means that your function isn't passed a copy of the array, it is passed a reference to the array itself.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question