First Arrays project...?
Dec 3, 2014 at 10:52pm UTC
I have an issue with my first arrays project! What I need to happen is have an array with 50 elements. The first 25 elements should be doubled while the last 25 should be cubed. Also the output should print to the console 10 elements per line...What I get is fairly close, but alas, not close enough! Any help would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// arrayproject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
// ==== Constants ====
const int ELEMENTS1 = 25;
const int ELEMENTS2 = 25;
// ===================
// ==== Function Prototypes ====
void FillArray(double []);
void FillArray2(double []);
void OutputArray(double []);
void OutputArray2(double []);
// =============================
// ==== Main Function ====
int main() {
// ==== Variables ====
double data[ELEMENTS1];
double data2[ELEMENTS2];
// ===================
FillArray(data);
FillArray2(data2);
OutputArray(data);
OutputArray2(data2);
return 0;
} // Function main()
// =======================
// ==== OutputArray Function ====
void OutputArray(double num1[ELEMENTS1])
{
for (int ii = 0; ii < ELEMENTS1; ii++)
{
for (int cc = 0; cc < 10; cc++)
{
cout << num1[ii] << " " ;
ii++;
}
cout << "\n" ;
}
} // OutputArray()
// ==============================
// ==== OutputArray2 Function ====
void OutputArray2(double num2[ELEMENTS2])
{
for (int ii = 0; ii < ELEMENTS2; ii++)
{
for (int cc = 0; cc < 10; cc++)
{
cout << num2[ii] << " " ;
ii++;
}
cout << "\n" ;
}
} // OutputArray2()
// ==============================
// ==== FillArray Function ====
void FillArray(double num1[ELEMENTS1])
{
for (int ii = 0; ii < ELEMENTS1; ii++)
{
num1[ii] = pow(ii + 1, 2);
}
} // FillArray
// ============================
// ==== FillArray2 Function ====
void FillArray2(double num2[ELEMENTS2])
{
for (int ii = 0; ii < ELEMENTS2; ii++)
{
num2[ii] = pow(ii + 1, 3);
}
} // FillArray2
// ============================
Dec 4, 2014 at 9:14am UTC
You don't need
OutputArray2(...)
, just call
OutputArray(data2 )
The problem is the loop on line 47: ii goes out of bounds. Change it like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// arrayproject.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <math.h>
using namespace std;
// ==== Constants ====
const int ELEMENTS1 = 25;
const int ELEMENTS2 = 25;
// ===================
// ==== Function Prototypes ====
void FillArray(double []);
void FillArray2(double []);
void OutputArray(double [], int count); // Note
// =============================
// ==== Main Function ====
int main() {
// ==== Variables ====
double data[ELEMENTS1];
double data2[ELEMENTS2];
// ===================
FillArray(data);
FillArray2(data2);
OutputArray(data, ELEMENTS1); // Note
OutputArray(data2, ELEMENTS2); // Note
return 0;
} // Function main()
// =======================
// ==== OutputArray Function ====
void OutputArray(double num1[], int count) // Note
{
for (int ii = 0; ii < count; ii++)
{
for (int cc = 0; cc < 10; cc++)
{
cout << num1[ii] << " " ;
ii++;
if (ii >= count) // Note
break ;
}
cout << "\n" ;
}
} // OutputArray()
// ==============================
// ==== FillArray Function ====
void FillArray(double num1[ELEMENTS1])
{
for (int ii = 0; ii < ELEMENTS1; ii++)
{
num1[ii] = pow(ii + 1, 2);
}
} // FillArray
// ============================
// ==== FillArray2 Function ====
void FillArray2(double num2[ELEMENTS2])
{
for (int ii = 0; ii < ELEMENTS2; ii++)
{
num2[ii] = pow(ii + 1, 3);
}
} // FillArray2
// ============================
Dec 4, 2014 at 2:46pm UTC
Thank you coder777, that fixed one of the issues! Now my only problem is that it should print 25 elements for each of the FillArray functions, however it only outputs 23 per function? any suggestions?
Dec 4, 2014 at 3:03pm UTC
The reason is that you have ii++
twice (on line 41/46). Remove the ii++
on line 41
Dec 4, 2014 at 10:01pm UTC
Thanks again coder777! everything is working great, except that my second array needs to start with 75 and then go up from there. Any suggestions!?
Dec 4, 2014 at 10:29pm UTC
Nevermind!! Figured it out! Thanks for all the help!
Topic archived. No new replies allowed.