Why are arrays so bad? advice please

The primary purpose of this lab is to implement a sorting algorithm on parallel arrays. There will be some sample code with the sort function left unimplemented. This sort function is to implement bubble sort on 3 parallel arrays. There will also be some code missing for reading input data and printing the sorted output.

The program will read names, ages and weights for up to 100 people. This means that you need 3 arrays of 100 elements each. Then it will sort the data based on decreasing weights and finally print a report.

The reason bubble sort works is that the for loop compares each pair of adjacent values of the x array. If any of the pairs are out of order, then these values are swapped with the generic swap function and the swapped variable is set to true. Eventually there will be a time when the for loop completes without swapping any values of x. At this point the array is sorted and the outer do-while loop will end since swapped will be false.

In your code for this lab you will adapt the bubble sort code to sort 3 arrays. The testing will be done only for the weights and swapping will be done on the weght values and corresponding values in the other 2 arrays.
Given appropriate input data you should produce a report like this one:
Name Age Weight
----------- --- ------
Fred 35 250.5
Barney 31 165.0
Betty 28 110.5
Wilma 33 115.0




Right now it crashes after it compiles and i enter a persons name. If someone could just help fix that ill try the rest on my own for now.


#include <iostream>
#include <string>
#include <cassert>

using namespace std;

const int MAX = 100;

int read_data ( string names[], int ages[], double weights[], int max )
{
int n;
//string input = "";
//
// Write this function first.
// Below here is the loop to read the input data.
//


for ( n = 0; n = MAX; n++){
cout << "Enter name" <<endl;
cin >> names[MAX];
cout << "Enter age" <<endl;
cin >> ages[MAX];
cout << "Enter weight" <<endl;
cin >> weights[MAX];
}

assert ( n > 0 && n <= MAX );
return n;
}

void sort ( string names[], int ages[], double weights[], int n )
{
assert ( n > 0 && n <= MAX );

//
// Write this function last.
// Below here is the bubble sort code.
//

//int i;
// bool swapped;
//
// do {
// swapped = false;
// for ( i = 0; i < n-1; i++ ) {
// if ( x[i] > x[i+1] ) {
// swap ( x[i], x[i+1] );
// swapped = true;
// }
// }
// } while ( swapped );

}

void print_report ( string names[], int ages[], double weights[], int n )
{
assert ( n > 0 && n <= MAX );

//
// Write this function second, so you can verify that you have read the data.
// Below here is the code to print the report.
//

int i;

for ( i = 0; i < n; i ++){
cout << names[i] << ages[i] << weights[i] << endl;
}

}

int main()
{
string names[MAX];
int ages[MAX];
double weights[MAX];
int n;

n = read_data ( names, ages, weights, MAX );
assert ( n > 0 && n <= MAX );
sort ( names, ages, weights, n );
print_report ( names, ages, weights, n );

system ("pause");
return 0;
}
Arrays are not 'bad'. They just aren't really convienient. Take for example a variable data structure. If that structure could contain 100 or 1000 items, then to use an array would be a waste of memory if we didnt fill every data block. In C++, it is better to use vectors, because the number of data blocks it can hold can be modified while the data is being given to the program. This means we arent taking up memory space that we aren't necessarily going to use. The reason your program is crashing could be one of these reasons:

- An array item is being called that does not exist. This means that your program is trying to access a memory address that isn't there.

- There is not enough memory to store 300kb of data (i hope that's not true)

- you are trying to pass an array to another function.

I did this... I ended up useing vectors in the end (so much easier that the memory properties of arrays) and dumping arrays. You need to use malloc to make the arrays work, and declare your functions as such. I can not tell you how, arrays and memory allocation are a bit too complex for me to learn on my own (i teach myself) and you are just a beginner.
Your program crashes because you are traying to access memory outside the array.

cin >> names[MAX];


There is no such element of the array as names[MAX] The acceptable range of elements is 0 : MAX - 1

I think you meant

cin >> names[n];
Last edited on
Topic archived. No new replies allowed.