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++)
{
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
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.
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
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)
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];
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)
#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 (autoconst &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 :)
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];
(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
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!