function/array

Please help. I posted in beginner as well, but not a lot of response so thought I'd see if I'd get a nibble here. I'm constructing a program that reads in from a text file into a multi dim array, takes the total from each row, then bubble sorts it in a separate function, then in ANOTHER function displays it in a nice neat little table. I have been working on this program for a week and I am still a beginner program but everyone I have asked for help so far has either sent me away or told me to go learn on my own. I have already dropped this major and at this point I just need assistance. my GPA is very important to me. i would love to learn it in the process but can someone at least just point me in the right direction?

I commented out the bubblesort i have going right now because it was crashing my whole program so obviously it is wrong. please any assistance would be awesome!

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<sstream>
using namespace std;


void getData(int arr2[][8], int length, int sum);
void bubbleSort(int[][8], int sum);

const int TTL_HRS = 7;


int main() {


getData({}, {}, 32);
/*bubbleSort({}, {});*/



system("pause");


}

void getData(int arr2[][8], int length, int sum) {


ifstream fin;
fin.open("empdata2.txt");
int i;
fin >> i;

if (fin.fail()) {
cout << " Your file was unable to open.\n\n";
exit(1);
}

int arr[50][8];
string name[50];

int total = 0;

for (int row = 0; row < i; ++row) {
fin >> name[row];
cout << name[row] << " ";

total = 0;

for (int day = 0; day < 7; day++) {
fin >> arr[row][day];
total += arr[row][day];
arr[i][TTL_HRS] = total;

cout << setw(3) << arr[row][day];

}

cout << setw(3) << total << endl;
cout << endl;

}
fin.close();
}

/*void bubbleSort(int arr[][8], int sum) {
int hours[TTL_HRS];
string name[50];
int temp = 0;

for (int i = 0; i < 50 - 1; i++)
{
for (int j = 0; j < 50 - 1; j++)
{

if (hours[j + 1] < hours[j])
{
temp = hours[j + 1];
hours[j + 1] = hours[j];
hours[j] = temp;

printf("%d\n", hours[j]);


}
}

}

}*/

Cat,Bill 9 3 7 5 8 0 0 32

Snake,Frank 2 3 8 3 6 3 5 30

Dog,Chet 8 8 3 0 8 2 0 29

Mouse,Mickey 9 10 4 7 0 0 0 30

Duck,Daffy 5 6 5 6 5 6 5 38

Mouse,Minnie 7 3 8 7 2 5 7 39

Yosimitee,Sam 2 5 3 0 4 9 4 27

Press any key to continue . . .//file it is coming from




what it is supposed to look like finished.

Employee Weekly Hours:
Name: S M T W T F S TTL
Mouse, Minnie 7 3 8 7 2 5 7 39
Duck, Daffy 5 6 5 6 5 6 5 38
Cat, Bill 9 3 7 5 8 0 0 32
Snake, Frank 2 3 8 3 6 3 5 30
Mouse, Mickey 9 10 4 7 0 0 0 30
Dog, Chet 8 8 3 0 8 2 0 29
Yosimitee, Sam 2 5 3 0 4 9 4 27

Last edited on
What does the function bubbleSort do?
what it is SUPPOSED to do is sort the total hours of each employee in descending order and move the names with their hours as well but i think starring at my computer for the last 6 days has fried my brain so honestly, at this point I just need help
Last edited on
what it is SUPPOSED to do is sort the total hours of each employee in descending order and move the names with their hours as well but i think starring at my computer for the last 6 days has fried my brain so honestly, at this point I just need help

In other words, you don't bother to learn anymore and you just want a full solution if available?
I don't want a full solution. i want help as to what to do next. I have been STUCK all day. i have been working on this project for a week and this is what i got. i have been asking where i can read to find solutions for this and haven't gotten any feedback so yes honestly at this point with my due date coming up and me just switching majors and caring much more about my GPA than programming, i would like examples of what could work or AT least some feedback that could point me in the right direction so i can actually finish this in the next 16 hours. every forum I've read, every book chapter i read, every slide my teacher posted was the absolute basics or didn't cover exactly what I need here, and I'm confused and lost. I posted what I have and I asked for the next step. I didn't post my homework and say please complete by this date.
I would like examples of what could work or AT least some feedback that could point me in the right direction so i can actually finish this in the next 16 hours.

16 hours is a little bit rush. You are definitely in a hurry.
That is all the time I have left at this point... I have been in front of my laptop non stop since I got out of class last Wednesday.
Hence the frustration and anxiety building up making it harder to focus at this point
@NavyVetSafIn
I actually have the same assignment. But my solution is different from yours, I can hardly tell you what to do next.
I ended up taking my bubble sort out completely and back to just the getData function. I need to figure this out. but it's a struggle. I think I am out of ideas
Update:

I have looked through some more of my slides and i was able to put this together

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<sstream>
using namespace std;

const int TTL_HRS = 7;
int getData(int arr2[][8], int length, int sum);
void bubbleSort(int name[], int total);


int main() {


getData({}, {}, 32);
bubbleSort({},32);


system("pause");


}

int getData(int arr2[][8], int length, int sum) {


ifstream fin;
fin.open("empdata2.txt");
int i;
fin >> i;

if (fin.fail()) {
cout << " Your file was unable to open.\n\n";
exit(1);
}

int arr[50][8];
string name[50];

int total = 0;
for (int row = 0; row < i; ++row) {
fin >> name[row];
cout << name[row] << " ";

total = 0;

for (int day = 0; day < 7; day++) {
fin >> arr[row][day];
total += arr[row][day];
arr[i][TTL_HRS] = total;

cout << setw(3) << arr[row][day];
int total = arr[row][TTL_HRS];

}

cout << setw(3) << total << endl;
cout << endl;

}
fin.close();
return total;
}
void bubbleSort(int name[],int total) {


int const SIZE = 50;
int names[SIZE], hours[SIZE];


for (int i = 0; i< SIZE; i--)
{
for (int j = 0; j< SIZE; j - 2)
{
if (names[j] > names[j + 1])
{
swap(names[j], names[j + 1]);
swap(hours[j], hours[j + 1]);
}
}
}
}


But I am still coming back with the same output. I don't have any errors and the output is
Cat,Bill 9 3 7 5 8 0 0 32

Snake,Frank 2 3 8 3 6 3 5 30

Dog,Chet 8 8 3 0 8 2 0 29

Mouse,Mickey 9 10 4 7 0 0 0 30

Duck,Daffy 5 6 5 6 5 6 5 38

Mouse,Minnie 7 3 8 7 2 5 7 39

Yosimitee,Sam 2 5 3 0 4 9 4 27

but unfortunately that means that my sort function isn't doing anything. can someone please tell me what I am doing wrong here?
What are the contents of the input file?

Note that you are not printing the sorted array, nor are you actually giving any data to the sort function to be sorted.

The only output comes from reading the file. The code you posted immediately prior to this doesn't compile.
Last edited on
7
Cat,Bill 9 3 7 5 8 0 0
Snake,Frank 2 3 8 3 6 3 5
Dog,Chet 8 8 3 0 8 2 0
Mouse,Mickey 9 10 4 7 0 0 0
Duck,Daffy 5 6 5 6 5 6 5
Mouse,Minnie 7 3 8 7 2 5 7
Yosimitee,Sam 2 5 3 0 4 9 4


is the input from the file. my assignment was due at noon today and my teach doesn't accept late work but I am actually still trying to figure this out (because it has been driving me mad)

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<sstream>

using namespace std;

int const TTL_HRS = 7;
void getData(int arr[][8], string name[50], int length);
void bubbleSort(int arr[][8], string name[50], int length);

int main() {

int arr[50][8];
arr[0][TTL_HRS];
string name[50];
getData(arr, name, 50);
bubbleSort(arr, name, 50);

system("pause");
}

void getData(int arr[][8], string name[50], int length) {

ifstream fin;
fin.open("empdata2.txt");
int i;
fin >> i;

if (fin.fail()) {
cout << " Your file was unable to open.\n\n";
exit(1);
}
arr[50][8];
name[50];
int total = 0;
for (int row = 0; row < i; ++row) {
fin >> name[row];
cout << name[row] << " ";

total = 0;

for (int day = 0; day < 7; day++) {
fin >> arr[row][day];
total += arr[row][day];


cout << setw(3) << arr[row][day];

}

cout << setw(3) << total << endl;
cout << endl;

}
fin.close();

}void bubbleSort(int arr[][8], string name[50], int length)
{
int total = arr[0][7];

for (int i = 0; i<50 - 1; i++)
{
for (int j = 0; j<8 - 1; j++)
{
//if(array[j+1][0] < array[j][0]) // swap values
if (arr[j + 1] > arr[j])
{
swap(arr[j], arr[j + 1]);
swap(name[j], name[j + 1]);
}
}
}



return;

}

it is supposed to read the input file, take the total from the 8th element ( which i had totaled already in the first function) and then put the table in order according to the total (using bubble sort in the second function) and then display in the 3rd function. ultimately it will be opening 2 files to do this but i think i know how to do that. i just seem to be really stuck at this bubble sort and have been for a while which has made me change my code many times (mostly for the worse)
Here's one possible solution.
Output goes to standard output, the input I used was
Mouse    , Minnie 7  3  8  7  2  5  7
Duck     , Daffy  5  6  5  6  5  6  5
Cat      , Bill   9  3  7  5  8  0  0
Snake    , Frank  2  3  8  3  6  3  5
Mouse    , Mickey 9  10 4  7  0  0  0
Dog      , Chet   8  8  3  0  8  2  0
Yosimitee, Sam    2  5  3  0  4  9  4


Note there was no leading `7' on the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>

/* Compute the total number of hours worked (is that the metric?)
   specified for the given entry. */
int entry_ttl(std::string entry) {
  /* Assumes that no numerals occur in the name field. */
  /* Cut the entry off at the beginning of the sequence of numbers. */
  auto it = entry.find_first_of("1234567890");

  /* If we didn't find a number in the string, ignore that string it. */
  if (it == std::string::npos) {
    return 0;
  }

  auto ss = std::stringstream{entry.substr(it)};

  auto ttl = int{};
  for (int day; ss >> day;) ttl += day;

  return ttl;
}

/* Sort a list of entries based on the number of hours worked. */
void bubble_sort(std::vector<std::string> &entries) {
  bool swapped = false;
  do {
    swapped = false;
    for (int i = 0; i < entries.size() - 1; ++i) {
      /* Run repeatedly through the array, sorting by their totals. */
      if (entry_ttl(entries[i]) < entry_ttl(entries[i + 1])) {
        entries[i].swap(entries[i + 1]);
        swapped = true;
      }
    }
  } while (swapped);
}

int main (int, char **) {
  /* Open up the file.  Crash with a system-error if something goes wrong. */
  auto f = std::ifstream("empdata2.txt");
  f.exceptions(std::ios::badbit);

  /* Make a place to store all the lines of the file. */
  auto lines = std::vector<std::string> {};

  /* Put each line `l' into `lines' -- the whole file. */
  for (auto l = std::string{}; std::getline(f, l);) {
    /* While we're at it, compute the total for each line (the assumption is
       that it's not there already in the input.) */
    lines.emplace_back(l + " " + std::to_string(entry_ttl(l)));
  }

  /* Could sort using a built-in, but let's not. */
  bubble_sort(lines);

  /* Print the sorted result to stdout */
  for (auto const &l: lines)
    std::cout << l << "\n";
}


I have to go out now, but I will be back about 6:30. If you have any questions or you want to break this down to use C-style arrays instead of more C++-y stuff, ask.

There's actually a small bug (because we're computing the a new total after adding the total; e.g., each total will be doubled while the line is being sorted) but it doesn't change the behavior of this program; I don't have time to fix it right now.
since we had to do it with arrays i was pretty determined to do it with arrays but thank you so much for your feedback! i have gotten it to sort. my next step is to display it pretty like in a separate function. this is where i am now :)

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<sstream>

using namespace std;

int const TTL_HRS = 7;
int getData(int arr[][8], string name[50], int length);
void bubbleSort(int arr[][8], string name[50], int& size);

int main() {
int size;
int arr[50][8];
arr[0][TTL_HRS];
string name[50];
size = getData(arr, name, 50);
bubbleSort(arr, name,size);

system("pause");
}

int getData(int arr[][8], string name[50], int length) {

ifstream fin;
fin.open("empdata2.txt");
int i;
fin >> i;

if (fin.fail()) {
cout << " Your file was unable to open.\n\n";
exit(1);
}
arr[50][8];
name[50];
int total = 0;
for (int row = 0; row < i; ++row) {
fin >> name[row];
cout << name[row] << " ";

total = 0;

for (int day = 0; day < 7; day++) {
fin >> arr[row][day];
total += arr[row][day];


cout << setw(3) << arr[row][day];

}
arr[row][TTL_HRS] = total;
cout << setw(3) << total << endl;
cout << endl;
}
return i;

fin.close();

}
void bubbleSort(int arr[][8], string name[50], int& size)
{
bool swapped = false;
do {
swapped = false;

for (int row = 0; row < size - 1; row++)

{
//if(array[j+1][7] < array[j][7]) // swap values
if (arr[row + 1][TTL_HRS] > arr[row][TTL_HRS])
{
swapped = true;
for (int day = 0; day < 7; day++){
swap(arr[row], arr[row + 1]);
}
swap(name[row], name[row + 1]);
}

}

}while (swapped == true);
for (int row = 0; row < size; ++row) {
cout << name[row] << " ";

for (int day = 0; day < 8; day++) {

cout << setw(3) << arr[row][day];

}

cout << endl;
}
}
(Sorry -- I'm back late.)
Simply change the line cout << name[row] << " "; to widen the name to a sufficient size: cout << setw(15) << name[row] << " ";
Yes that is awesome! But unfortunately not where I am having my brain fart. I have gathered my info. my original goal was to use the first function to bring the info in...CHECK 2nd function sorts it...CHECK (5 years later haha) and the third function is supposed to display everything.

Name S M T W T F S TTL
Mouse,Minnie 7 3 8 7 2 5 7 39
Duck,Daffy 5 6 5 6 5 6 5 38
Cat,Bill 9 3 7 5 8 0 0 32
Snake,Frank 2 3 8 3 6 3 5 30
Mouse,Mickey 9 10 4 7 0 0 0 30
Dog,Chet 8 8 3 0 8 2 0 29
Yosimitee,Sam 2 5 3 0 4 9 4 27

or something similar to that is the goal
i got it!!!!!!!!!!!!!!!!! of course a day too late...but i got it! well almost. i still have to do one more thing to it but it's a running code that works which is a lot more than i had yesterday!
Topic archived. No new replies allowed.