multiplying 1D Array with a 5D Array

Hi, i am currently doing a final assignment for my programming module at college and i am struggling to get the correct output for one part of the program and the lecturer is unable to help us with it. Our class had just started looking at arrays when we were given the assignment

the overall program works with 4 modules but the section of code i am stuck with uses only 2 of the modules but it could probably get by with just the 1 as i had created the 2nd to help with another loop.

the problem I am having is the array either multiplies the total of each array, which is around 5x bigger then it should be, add the total of arrays together or come up with a completely different answer when ever i try a different combination with the part of the code. The program that i have used to create it is BCC55 by Borland.

below is the section of code that isn’t working along with the modules that are used in the code


#include <iostream.h>
#include <conio.h>
#include <M:\Assignment\CaseStudy\ArrayAdder.cpp>
#include <M:\Assignment\CaseStudy\ArrayAdder2.cpp>


const int iRows = 5;
const int iCols = 6;

void main()
{
int iTotalFound, iRound, iRound1, iValue;


int iArray1 [iRows][iCols] = {{120,125,110,90,130,50 },
{137,138,150,56,136,100},
{103,133,105,80,109,150},
{67 ,66 ,90 ,70 ,71,180},
{57 ,80 ,99 ,85 ,90,110}};


int iArray2 [iRows] = {475,524,691,832,999};

int iArray1D [iRows*iCols] = {0};
int iArray2D [iRows*iCols] = {0};

for (iRound=0; iRound<iRows; iRound++)
{
for (iRound1=0; iRound1<iCols ; iRound1++)
{
iArray1D[iRound * iCols + iRound1] = iArray1[iRound][iRound1];

}
iArray2D[iRound]= iArray1[iRound][iRound1] * iArray2[iRound];
}


iTotalFound = ArrayAdder(iArray1D, iRows*iCols);
iValue = ArrayAdder2(iArray2D, iRows);

cout << "The Total number of computers sold in total is " << iTotalFound << endl;
cout << "and the value is " << iValue << endl;

getch();
}


the code for the ArrayAdder module is the same for both ArrayAdder1 and ArrayAdder2:

int ArrayAdder(int iArray[], int iLength)
{
int iIndex;
int iTotal=0;

for (iIndex = 0; iIndex <iLength; iIndex++)
{
iTotal = iTotal + iArray[iIndex];
}

return iTotal;
}



to help i have made a table that has all the correct outputs for the program in. so i know which are the correct and incorrect outputs

the correct out put is for the iTotalFound is 3087, while the correct output for the iValue is supposed to be 2,115,550

incorrect outputs for iValue include 687293 and 10869327.

i also have an idea to ry and fix it but i don’t know how to implement it. The solution I have is to use a previous section of code to create an add loop for the array til until the counter hits 6 when it will stop adding to the iValue. I have worked out that I will probably need the following line of code with some value in the array part:

iValue = iValue + (iTotalFound * iArray2[ ])

below is the code from a previous loop that is working correctly. It include the choice selection and cout code:

cout << "Choose one of the following Products" << endl;
cout << "1. Axus First" << endl;
cout << "2. Axus Advanced" << endl;
cout << "3. Axus Performance" << endl;
cout << "4. Axus Proffessional" << endl;
cout << "5. Axus Deluxe" << endl;

cin >> iChoice;

for (iRound = 0; iRound < iCols; iRound ++)

{
iArray1D[iRound]= iArray1[iChoice-1][iRound];
}

iTotalFound = ArrayAdder(iArray1D, iCols);

iValue = iTotalFound * iArray2[iChoice -1];

cout << "The Total of Product " << iChoice << "sold is " << iTotalFound <<" and the value is GBP " << iValue << endl;


I have tried looking on the internet and reading the books but I have had trouble understanding some of the things mentioned.

I will take and appreciate any and all help that can be offered

Thank you

Kevin

Email(if needed): littlemitch3@gmail.com
Last edited on
Topic archived. No new replies allowed.