Hi everyone,
I need a help for the following task, please:
A series of files have been sent to your program with different memory sizes. The sizes are all in whole numbers of Megabytes (MB) but the number of files are unknown. You have been asked to create a program that will find the file with the largest memory size
Notes:
• You must make use of methods throughout
One method will display the array
A second method will carry out the functionality of the task. Find Largest storage space
• You should globally declare both the array (storSpace[]) and the number of array elements used for the counter.
• The global items above must also be initialised in their declaration
• There should be no menu
• There is no requirement for parameter passing
• Do not assign an array size (number of elements to the array)
• The values to be used for the array are
232,241,324,216,221,298,334,212,235,324,264,141
[#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//global declaration of arrays
int storSpace[] = { 232,241,324,216,221,298,334,212,235,324,264,141 }; //dynamic allocation of data to num_elements[] array
int num_elements = sizeof(storSpace) / sizeof(int);
// method declarations
void display_array(); // for displaying
void find_biggest(); // finding the biggest size
void display_array()
{
int count = 0;
for (count = 0; count < num_elements; count++)
cout << " " << storSpace[count] << endl;
}
void find_biggest()
{
int biggest_array = storSpace[0];
// using standart algorithm code for the biggest
for (int count = 1; count < num_elements; count++)
{
if (storSpace[count] > biggest_array)// the largest file size
{
biggest_array = storSpace[count];
}
}
cout << "The biggest array is " << storSpace[biggest_array] << ". The file was " << storSpace[biggest_array] << endl;
} // end biggest array
void main()
{
display_array();
find_biggest();
cin.get(); //to show the screen
}
]
I changed the code and it works but does not output the biggest array. It should shows the biggest array. If somebody can help I will be highly appreciated. Thank you.
Ho Thomas,
I changed the code (the code is edited here) and there isnt an error now but the console does not output the biggest array. Instead it shows 0 for biggest array.
Thank you for the quick reply.
I think the problem is that you store the biggest value in biggest_array, but output some value in storSpace that is far outside the bounds. Imagine that biggest_array is 334. That would be storSpace[334].
1 2
cout << "The biggest array is " << storSpace[biggest_array] << ".
The file was " << storSpace[biggest_array] << endl;