For context, I can't use anything that isn't taught in csc101 (what you learned may have been different) so I can't use things like vectors, structs, and classes. More context, I have an assignment which requests I have a function which takes an array numarray
with random values, and removes the values from 20 to 40. As I understand it, the best way to do that is to make a new array and take the valid values from numarray
and put them in a new array temparray
. I tried implementing this the best way I could figure up, but it seems to only spit out a set number which is a long negative number over and over in a loop. I know it is this function because when not called I don't have a problem. The problem also doesn't occur if I comment out the while loop at the end of the function. I will first attach the function in question, and then the whole of the program for added context. Open to any criticism, but passing the class is my priority over elegance, and efficiency. If the professor wants something done a certain way, I must oblige. Thanks for your time.
The required function:
void Delete(int* numarray, int *temparray) {
int arrayindex = 0;
for (int index = 0; index < 100; index++) {
if (numarray[index] < 20 && numarray[index] > 40) {
temparray[arrayindex] = numarray[index];
} arrayindex++;
}
cout << arrayindex << endl;
cout << temparray[arrayindex] << endl;
while (arrayindex <! 0) {
cout << temparray[arrayindex - 1] << endl;
}
}
The whole project:
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <cstddef>
#include <array>
using namespace std;
ofstream randomData;
ifstream inputrandomData;
void randomgenerator();
void read(int *numarray);
void printArray(int *numarray);
void searchArray(int* numarray);
void Delete(int* numarray, int* temparray);
void randomgenerator() {
srand(time(0));
randomData.open("randomData.txt");
for (int counter = 0; counter < 100; counter++) {
randomData << rand() % 100+1 << endl;
}
randomData.close();
}
void read(int *numarray) {
inputrandomData.open("randomData.txt");
for (int i = 0; i < 100; i++) {
inputrandomData >> numarray[i];
}
inputrandomData.close();
}
void printArray(int *numarray) {
for (int index = 0; index < 100; index++) {
cout << numarray[index] << endl;
}
}
void searchArray(int* numarray) {
int searchedArray[6] = {};
for (int index=0; index < 100; index++) {
if (numarray[index] > searchedArray[0]) {
searchedArray[0] = numarray[index];
searchedArray[1] = index;
}
}
for (int index = 0; index < 100; index++) {
if (numarray[index] > searchedArray[2] && numarray[index] < searchedArray[0]) {
searchedArray[2] = numarray[index];
searchedArray[3] = index;
}
}
for (int index = 0; index < 100; index++) {
if (numarray[index] > searchedArray[4] && numarray[index] < searchedArray[2]) {
searchedArray[4] = numarray[index];
searchedArray[5] = index;
}
}
cout << "Largest Number: " << searchedArray[0] << " " << "Index: " << searchedArray[1] << endl;
cout << "Second Largest Number: " << searchedArray[2] << " " << "Index: " << searchedArray[3] << endl;
cout << "Third Largest Number: " << searchedArray[4] << " " << "Index: " << searchedArray[5] << endl;
}
void Delete(int* numarray, int *temparray) {
int arrayindex = 0;
for (int index = 0; index < 100; index++) {
if (numarray[index] < 20 && numarray[index] > 40) {
temparray[arrayindex] = numarray[index];
} arrayindex++;
}
cout << arrayindex << endl;
cout << temparray[arrayindex] << endl;
while (arrayindex <! 0) {
cout << temparray[arrayindex - 1] << endl;
}
}
int main() {
int numarray[100] = {};
int temparray[100] = {};
randomgenerator();
read(numarray);
printArray(numarray);
searchArray(numarray);
Delete(numarray, temparray);
return 0;
}