Hi, I am trying to put random numbers in array, and use the array for other functions. However, I am stuck on inputting and displaying that array with random numbers. Can someone help? Please ignore other cases, and focus on case 0, which is displaying the elements of the array.

I have included header and cpp file.
Header file:

//Header File for Sorting.cpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <ctime>
#include <cstdlib>

using namespace std;

class Sort{
    clock_t start,finish;   // Clock

public:
// (0) View unsorted list
        void display(int array) {
        cout << array << endl; 
    }
}

CPP File:

#include "Sorting.h"

int main () {
    cout << "Main Start" << endl;

//double produce(int *array) {
// 1st Argument
    int array[1000],n,N,input;
    cout << "Input total # of numbers to be sorted: "; 

    // Output Unsorted Numbers
    ofstream myfile;
    myfile.open ("input-N.txt");

    srand((unsigned)time(0)); 
    cin >> N;   // Input # of numbers to be sorted

    for(int i=0; i<N; i++) { 
        array[i] = (rand()%10000)+1; // Random number of 100 <= N <= 10,000
        myfile << array[i] << endl;
    }
    return *array;
    myfile.close();


// 2nd Argument
    {
    cout << "\n\nPick a Computing Mode:\n";
    cout << "0. View Unsorted array\n";
    cout << "1. Exit\n";
    cout << "\nSelection: ";
    cin >> input;

    switch (input) {
        Sort Sorting;
        case 0:
            Sorting.display(array);
            cout << "Displaying Unsorted array\n";
            break;

        case 1: 
            cout << "Exiting............."; 
            break;

        default: cout << "\nInvalid Choice";
            break;
        }
    cin.get();
    }
}

Recommended Answers

All 8 Replies

You can't just cout << array and expect anything to happen. An array is a collection of items. You have to output each item.

I did some work on this, and I made it display, but the array only displays the last element... How to display the whole array of random numbers?

Header file

//Header File for Sorting.cpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <ctime>
#include <cstdlib>

using namespace std;

class Sort{
    clock_t start,finish;   // Clock

public:
// (0) View unsorted list
        void display(int x[],int N) {
        cout << "Unsorted list\n";

        for(int i=0; i<N; i++) { 
        cout << *x << endl; 
        }
        system ("pause");
    }
};

CPP File:

#include "Sorting.h"

int main () {
    cout << "Main Start" << endl;

//double produce(int *array) {
// 1st Argument
    int x[1000],N,input;
    cout << "Input total # of numbers to be sorted: "; 

    // Output Unsorted Numbers
    ofstream myfile;
    myfile.open ("input-N.txt");

    srand((unsigned)time(0)); 
    cin >> N;   // Input # of numbers to be sorted

    for(int i=0; i<N; i++) { 
        x[i] = (rand()%10000)+1; // Random number of 100 <= N <= 10,000
        myfile << x[i] << endl;
        cout << x[i] << endl;
    }
    myfile.close();




// 2nd Argument
    {
    cout << "\n\nPick a Computing Mode:\n";
    cout << "0. View Unsorted array\n";
    cout << "1. Exit\n";
    cout << "\nSelection: ";
    cin >> input;

    switch (input) {
        Sort Sorting;
        case 0:
            Sorting.display(x, N);
            cout << "Displaying Unsorted array\n";
            break;

        case 1: 
            cout << "Exiting............."; 
            break;

        default: cout << "\nInvalid Choice";
            system ("pause");
            break;
        }
    cin.get();
    }
}

how do you output each item of array???? This is so confusing :(:(

What is *x? How is it related to i? Or how it i related to *x?

I tried putting x and it displayed hexa-code so I tried *x and it displayed actual number.
Anyways, x[] should be array I want to use, and i should be elements in it? Is this good explanation?

What I don't understand is in main function,

    for(int i=0; i<N; i++) { 
        x[i] = (rand()%10000)+1; // Random number of 100 <= N <= 10,000
        myfile << x[i] << endl;
        cout << x[i] << endl;

    }

displays what I want.

and when I use case 0, it displays N number of first element of x[i]..

wait I think I did it. I changed in the header file, cout << *x to x[i] and it worked.... I think

*x is a dereferencing a pointer or it gives the value the pointee ( pointer points to pointee ) is holding.

What you have done previously was printing the first value of the array. cout << *x is equivalent to cout << x[0].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.