My issue is on line 73 through 99. What i cant seem to figure out is how to loop through an array and copy all the non repeating numbers and place them in another array? each time ive done it, it will either give me random numbers or only display the repeating numbers
i have my main array which is called alpha and my other array which will hold my non repeating numbers called diffAlpha.
and im supposed to be using dynamic arrays.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
usingnamespace std;
int main()
{
int *alpha;
alpha = newint[50]; //dynamic array alpha
int num = 0; //used to determine how big diffAlpha will be since we dont know how many numbers will be placed in the array
int *diffAlpha = newint[num];
std::fill(alpha, alpha + 50, -1); //set all elements to -1
cout << "Output the value of the first component of the array alpha." << endl;
cout << alpha[0] << endl;
alpha[25] = 62;
alpha[10] = (alpha[49] * 3) + 10;
cout << "Use a for loop to output the value of a component of alpha if its index is a multiple of 2 or 3" << endl;
for (int i = 0; i < 50; i++)
{
if (!(alpha[i] % 2))
{
cout << alpha[i] << endl;
}
if (!(alpha[i] % 3))
{
cout << alpha[i] << endl;
}
}
cout << "Output the value of the last component of alpha." << endl;
cout << alpha[49] << endl;
cout << "Output the value of the alpha so that 15 components per line are printed." << endl;
for (int i = 0; i < 15; i++)
{
cout << alpha[i] << " ";
}
cout << endl;
for (int i = 15; i < 30; i++)
{
cout << alpha[i] << " ";
}
cout << endl;
for (int i = 30; i < 45; i++)
{
cout << alpha[i] << " ";
}
cout << endl;
cout << "Use a for loop to increment every other element (the even indexed elements)." << endl;
int count = 0;
for (int i = 0; i < 50; i++)
{
if (!(alpha[i] % 2))
{
count++;
if (count % 2)
{
cout << alpha[i] << " ";
}
}
}
cout << endl;
cout << "Create a new array, diffAlpha, whose elements are the differences between consecutive elements in alpha" << endl;
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
if (alpha[i] == alpha[j])
{
int value = 0; //Where the number will be placed in the array.
diffAlpha[value] = alpha[i];
value += 1;
num++; //increment num value to determine size of the array.
}
}
}
cout << endl;
for (int i = 0; i < num; i++)
{
cout << diffAlpha[i] << " ";
}
system("Pause");
return 0;
}
Lines 14-15: newint[0]; // error
How many elements are in an array that has 0 elements?
Make a temporary array of 50 elements. Copy the uniques.
Now you know how long the diffAlpha must be. Create it so.
Copy from temporary to diffAlpha and then remove the temporary.