Teny
(╯°□°)╯︵ â”»â”â”»
- Messages
- 5,957
- Location
- United States
Here is the assignment description
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.
I ran the code 3 times, and this is what I got.
Here's the skeleton by the way.
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.
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.
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.