Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-26-2011, 07:59 PM   #1
(╯□)╯︵ ┻━┻
 
Teny's Avatar
 
Join Date: Dec 2006
Location: United States
Posts: 5,957
Default Need help with HW (Arrays) C++

Here is the assignment description
Quote:
Write a program that will populate an array of random numbers between 0 and 1410, print the array contents and memory locations, and include function that will return a pointer to the smallest number in an array. All iteration must be done using pointer arithmetic

The program must implement the functions as defined in the pa6-skeleton.cpp file:

int* find_smallest_number(int*, int);
void fill_with_random_data(int*, int);
void print_array(int*, int);

The output should look like this:

Array contents are: 68 [0x7fff69f4e71c], 1352 [0x7fff69f4e720], 989 [0x7fff69f4e724], 27 [0x7fff69f4e728], 568 [0x7fff69f4e72c], 542 [0x7fff69f4e730], 1125 [0x7fff69f4e734], 810 [0x7fff69f4e738], 1324 [0x7fff69f4e73c], 660 [0x7fff69f4e740]
The smallest item is 27 and it is at memory location is 0x7fff69f4e728
I've tried this for awhile, and got stuck at a certain point. I got the random number generator, but its always in the same memory allocation. Here is what I got so far.

Code:
#include <iostream>
#include <cstdlib>
using namespace std;

void fill_with_random_data (int*, int);
void print_array (int*, int);

int main()
{
        const int size = 10;
        int random_array[size] = {};
        fill_with_random_data (random_array, size);
        print_array (random_array, size);
}

void fill_with_random_data (int *random_array, int size)
{
        for (int i=0; i<size; i++)
        {
                srand(time(0));
                random_array[i] = rand() % 1410;
        }
}

void print_array (int* random_array, int size)
{
        for (int i=0; i<size; i++)
        {
        cout <<"Array contents are: " << random_array[i] << ' ' << &random_array[size] << '\n';
        }
}
I ran the code 3 times, and this is what I got.
Quote:
First time
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608
Array contents are: 183 0xbfe72608

Second time
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18
Array contents are: 1125 0xbfe5ad18

Third time
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Array contents are: 1121 0xbff7f358
Here's the skeleton by the way.
Code:
// CSCI 1410 Programming assignment 6
// Written by FIRSTNAME LASTNAME <EMAIL>
#include <iostream>
#include <cstdlib>
using namespace std;

int* find_smallest_number(int*, int);
void fill_with_random_data(int*, int);
void print_array(int*, int);

int main()
{
    const int size = 10;
    int random_array[size] = {};
    fill_with_random_data(random_array, size);
    print_array(random_array, size);

    int* smallestNumber = find_smallest_number(random_array, size);

    if (smallestNumber < &random_array[0] || smallestNumber > &random_array[size])
    {
        cout << "ERROR : The pointer returned is outside the random array!" << endl;
    }
    else
    {
        cout << "The smallest item is " << *smallestNumber;
        cout << " and it is at memory location is " << smallestNumber << endl;
    }

}

// This function takes an array and fills it with random numbers
// between 0 and 1410.  It also seeds the random number generator to
// ensure reasonable random numbers.
void fill_with_random_data(int *random_array, int size)
{
    //
    //
    // IMPLEMENTATION GOES HERE
    //
    //
}

// This function returns a pointer to the smallest number in
// the array.
int* find_smallest_number(int *random_array, int size)
{
    //
    //
    // IMPLEMENTATION GOES HERE
    //
    //
}

// This function prints every value of the array along with
// the memory location.
void print_array(int *random_array, int size)
{
    //
    //
    // IMPLEMENTATION GOES HERE
    //
    //
}
NOTE* I am not asking people to do my HW for me. This HW is already over a week late. It was work 10 points, and I lose 1 point for each day late. If I were to turn it in tonight or tomorrow, I will get a max of 1 point for doing it right.
__________________
Desktop -Cooler Master NV690/AMD Phenom II X4 965 BE/MSI 880G-E45 Motherboard/2 x 2GB DDR3 G.Skill Ripjaws X/Sapphire HD 5870 1GB GDDR5/CORSAIR CMPSU-550VX/Seagate 7200.12 500GB (OS), Samsung F3 1TB (Storage)/Sony DVD Burner/Windows 7 Professional 64-bit
Teny is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:04 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0