Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

7
C++ hw for class, almost got my code doing everything I want just need a little help
Post Flair (click to view more posts with a particular flair)
Post Body

So in my code I need help on trying to implement that if the user does not create the array first then the user will not be able to do any of the other options first in the menu.

here is my code

#include <iostream>
#include <cmath>

using namespace std;

int counter = 0;
int add = 0;
int size;

void create_arr(int arr_size, int *grades) {
    for(int i = 0; i < arr_size; i  ) {
        grades[i] = 0;
    }
    for(int i = 0; i < arr_size; i  ) {
        if(i < arr_size - 1) {
            cout << grades[i] << ", ";
        }
        else {
            cout << grades[i];
        }
    }
    cout << endl;
}

void add_Grade(int score, int *grades) {
    for(int i = add; i < size;) {
        grades[i] = score;
        break;
    }
    add  ;
    counter   ;
    cout << "Grade added \n";
}

void print(int *grades) {
    cout << "Grades = ";
    for(int i = 0; i < size; i  ) {
        if(i < size - 1) {
            cout << grades[i] << ", ";
        }
        else {
            cout << grades[i] << " \n";
        }
    }
    cout << endl;
}

void delete_Grade(int deleteG, int *grades, int arrSize) {
    int count = 0;
    for(int i = 0; i < arrSize; i  ) {
        if(grades[i] == deleteG) {
            for(int j = i; j < (arrSize - 1); j  ) {
                grades[j] = grades[j 1];
                count  ;
            }
        }
    }
    if(count == 0) {
        cout << "Grade not found\n";
    }
    else {
        size -= 1;
        cout << "Grade Deleted\n";
    }
}

int Bytes(int arrsize, int *grades) {
    int unusedMem;
    int count = 0;
    for(int i = 0; i < arrsize; i  ) {
        if(grades[i] == 0) {
            count  ;
        }
    }
    unusedMem = count * sizeof(int);
    return unusedMem;
}

void order(int order, int *grades, int size) {
    if(order == 1) {
        for(int i = 0; i < size; i  ) {
            for(int j = i   1; j < size; j  ) {
                if(grades[i] > grades[j]) {
                    int temp = grades[i];
                    grades[i] = grades[j];
                    grades[j] = temp;
                }
            }
        }
        cout << "Grades sorted in ascending order \n";
    }
    else {
        for(int i = 0; i < size; i  ) {
            for(int j = i   1; j < size; j  ) {
                if(grades[i] < grades[j]) {
                    int temp = grades[i];
                    grades[i] = grades[j];
                    grades[j] = temp;
                }
            }
        }
        cout << "Grades sorted in descending order \n";
    }
    cout << endl;
}

int main() {
    int input = 0;
    int num_add;
    int deleteGrade, orderOption;
    int *grades = new int[input];
    cout << "(1) Create array, (2) Add grade, (3) Delete grade, \n(4) Print array, (5) Unused memory, (6) Sort array, \n(7) Exit: ";
    cin >> input;
    cout << endl;


    while(input != 7) {
        switch(input) {
            case 1:
                cout << "Enter the array size: ";
                cin >> size;
                create_arr(size, grades);
                break;
            case 2:
                if(counter >= size) {
                    size = pow(size,2);
                    int *temp = new int[size];
                    for(int i = 0; i < counter; i  ) {
                        temp[i] = grades[i];
                    }
                    for(int j = counter; j < size; j  ) {
                        temp[j] = 0;
                    }
                    delete [] grades;
                    grades = temp;
                    delete [] temp;
                }
                cout << "Enter the new grade: ";
                cin >> num_add;
                add_Grade(num_add, grades);
                break;
            case 3:
                cout << "Enter the grade to be deleted: ";
                cin >> deleteGrade;
                delete_Grade(deleteGrade, grades, size);
                break;
            case 4:
                print(grades);
                break;
            case 5:
                cout << "Unused MEM space in bytes = ";
                cout << Bytes(size, grades);
                cout << endl;
                break;
            case 6:
                cout << "(1) Ascending, (2) Descending: ";
                cin >> orderOption;
                order(orderOption, grades, size);
                break;
            default:
                break;
        }
        cout << endl;
        cout << "(1) Create array, (2) Add grade, (3) Delete grade, \n(4) Print array, (5) Unused memory, (6) Sort array, \n(7) Exit: ";
        cin >> input;
        cout << endl;
    }

    return 0;
}

Author
Account Strength
100%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
626
Link Karma
73
Comment Karma
235
Profile updated: 5 days ago
Posts updated: 8 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago