writing values to an array from another array excluding unwanted values c++

Related searches

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;
}

Here was the solution I was prompted to come up with. There were a couple logical flaws. First, my conditional would never be true as I used the && operator which means the number would have to be lower than 20 AND greater than 40. Changed that to || operator to check for one or the other. Then, as stated in the comments, I had been overthinking it by creating another array. You have to have two different index counters in order to read from the original data set, and to write the new data set in behind it. Now, when ran, the numarray takes values at the numarray[arrayindex] which is only incremented when the conditional is called, and reads from numarray[index] which increments on every for loop. Here is the edited function below:

void Delete(int* numarray) {

    int arrayindex = 0;
    for (int index = 0; index < 100; index++) {
        if (numarray[index] < 20 || numarray[index] > 40) {
            numarray[arrayindex] = numarray[index];
            arrayindex++;
        }   
    }
    cout << arrayindex << endl;
    for (int newindex = 0; newindex < arrayindex; newindex++) {
        cout << numarray[newindex] << endl;
    }
}

I removed temparray from the program entirely. Thanks for the help.

Delete an element from array (Using two traversals and one , Given an array and a number 'x', write a function to delete 'x' from the given array. We assume that array maintains two things with it, capacity� Required knowledge. Basic Input Output, For loop, Array. Logic to copy array elements to another array. Step by step descriptive logic to copy an array. Input size and elements in array, store it in some variable say size and source.

You can break up the problem into many pieces at first so that it is easier to program and understand. If you're struggling, keep splitting up the task into smaller simpler functions, like this:

// remove a range of values from arr
void remove_range(int *arr, int size, int from, int to) {
  for (int r = from; r <= to; r++) {
    remove_all(arr, size, r);
  }
}    

// remove all of a single value from arr
void remove_all(int *arr, int size, int value) {
  for (int i = 0; i < size; i++) {
    if (arr[i] == value) {
      remove_one(arr, size, value);
    }
  }
}

// remove the first occurrence of a value from arr
void remove_one(int *arr, int size, int value) {
  for (int i = 0; i < size; i++) {
    if (arr[i] == value) {
      remove_i(arr, size, value);
    }
  }
}
// remove the value at an index of arr by shifting all the entries after it left one
// and putting a 0 at the end in case this is the first removal
void remove_i(int *arr, int size, int i) {
  for (; i < size - 1; i++) {
    arr[i] = arr[i + 1];
  }
  arr[i] = 0;
}

Arrays \ Processing.org, Let's call this array “dates” and store the values in sequence: Using what we know about drawing without arrays, ten variables are needed to store the data; each The elements in the array remain in the same position once they are written, but is especially useful with larger arrays, to avoid unnecessary copying of data� Given an array of N unique numbers. Also given two numbers a and b such that a will always be before b in the array. The task is to find the sum of the array elements excluding the elements which lie between a and b.

array_diff - Manual, Compares array1 against one or more other arrays and returns the values in array1 I sought a function, which (mathematically) computes A - B, or, written a handy way of deleting array elements by their value, without having to unset it by With the normal DIFF I can't, because if I've an array like C=1,4, I dont know if� More Topics on Arrays in C: 2D array – We can have multidimensional arrays in C like 2D and 3D array. However the most popular and frequently used array is 2D – two dimensional array. In this post you will learn how to declare, read and write data in 2D array along with various other features of it.

array_merge - Manual, If the input arrays have the same string keys, then the later value for that key will 7.4.0, This function can now be called without any parameter. Creates an array by using one array for keys and another for its values; array operators $c = array_merge($a, $b); // shows a PHP warning: Argument #2 is not an array You need to read the array, use Replace Array Subset to change the value, and then write the array back in. But I would be very careful with all of this. If you are using local variables and/or property nodes to access the value of an array, then you run the risk of race conditions where multiple locations could be stepping on each other trying

Arrays � The Julia Language, Element type T must be able to hold these values, i.e. Nothing <: T . Element values can be specified using Type[a,b,c,. concurrent access to the elements of a BitArray where at least one of them is a write is not thread safe. array (it is a subtype of AbstractArray ), but the underlying data is actually part of another array . To filter a data in an array formula (to exclude or require certain values), you can use an array formula based on the IF, MATCH, and ISNUMBER functions. In the example shown, the formula in H5: { = COUNT ( IF ( ISNUMBER ( MATCH ( data , filter , 0 )), data )) }

C program to find the maximum and minimum element in an array – In this article, we will brief in on the many ways to find the maximum and minimum element in an array in C programming. Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly.

Comments
  • There are multiple bugs in the shown code. Let's start with the first bug: "if (numarray[index] < 20 && numarray[index] > 40)". Can you give me any number that's less than 20 and more than 40? What kind of a number would this be? Remember the Golden Rule Of Computer Programming: "your computer always does exactly what you told it to do instead of what you want it to do". What did you tell your computer to do here?
  • Ah dang. i want to facepalm so hard right now. Overthinking again. Thanks very much.
  • That's not the only one, there's more that you will have to find and fix. Just remember The Goldern Rule, and it should be easy to figure everything out.
  • @SamVarshavchik I fixed that. Now I see why even when I called a specific index like temparray[1] I was still getting a dummy value. Alright, now I know my problem lies in the way I am incrementing, because if there is no value on the index printed, it will output a dummy value.
  • I believe you are missing the point of the exercise by using temparray. I will write up a quick answer for you. By the way, you wrote a nicely formatted question that makes it easy to help you.
  • I can see how that would work. Instead of looking for valid values, instead remove the invalid ones. Thanks for the input.