Question

where is the error in my code? it wont print all positions in the array for...

where is the error in my code? it wont print all positions in the array for ascending (ralph is missing) or when it lists who got onto the elevator. This is C++.

Input Data

Anne 150
Bob 250
Ralph 305
Tim 250
Barbara 85
Jane 160
Steve 180
Tom 210
Mike 165
Shirley 155
Pam 125
Frank 130

Code

#include <iostream>
#include <fstream>

using namespace std;

/* The purpose of this program is to compare different
ways for people to enter an elevator efficiently by
comparing in regular order, descending order, and ascending order. */
ifstream fin ("inputData.txt");
int size = 12;
string name [20];
int weight [20];
int method [20];
int methodcount = 0;

void filldata ()
{
for(int i = 0; i < size; i++)
{
fin >> name[i];
fin >> weight[i];
}
}
//print array
void print ()
{
cout << "Name\tWeight" << endl;
cout << endl;

for(int i = 0; i < size; i++)
{
cout << name[i] << "\t" << weight[i]<<endl;
}
}

void whogoeson ()
{
int totalweight = 0, i = 0;

cout << "-----Elevator Riders-----" << endl;
cout << endl;
cout << "Name" << "\t" << "Weight" << endl;

while (totalweight < 1100)
{
totalweight += weight[i];
i++;
if (totalweight < 1100)
    {
        cout << name[i] << "\t" << weight[i]<<endl;
    }
}
cout << endl;
cout << "Total Weight: " << (totalweight - weight[i]) << endl;
cout << "Number of Elevator Riders: " << (i-1)<<endl;
method[i-1] = totalweight-weight[i];

}

void ascending ()
{
for(int i = size;i > 0;i--)
{
for(int j = 0; j < i; j++)
{
if(weight[j] > weight[i])
{
int hold = weight[j];
weight[j] = weight[i];
weight[i] = hold;

string hold2=name[j];
name[j]=name[i];
name[i]=hold2;
}
}
}
}

void descending()
{
for(int i=size;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(weight[j]<weight[j+1])
{
int hold=weight[j];
weight[j]=weight[j+1];
weight[j+1]=hold;

string hold2 = name[j];
name[j] = name[j+1];
name[j+1]=hold2;
}
}
}
}

int main()
{
cout << "-----Method "<<methodcount+1<<" -----";
cout << endl;
filldata();
print();
cout << endl;
whogoeson();
cout << endl;
methodcount++;

cout << "-----Method "<<methodcount+1<<" -----";
cout << endl;
cout << "Ascending order by Weight:"<<endl;
cout << endl;
ascending();
print();
cout << endl;
whogoeson();
cout << endl;
methodcount++;

cout << "-----Method "<<methodcount+1<<" -----";
cout << endl;
cout << "Descending order by Weight:"<<endl;
cout << endl;
descending();
print();
cout << endl;
whogoeson();
cout << endl;

cout << "The best Method was " << " "<<endl;
cout << endl;

cout << "Coded by YOUR NAME" << endl;
return 0;
}

Output

-----Method 1 -----
Name Weight

Anne 150
Bob 250
Ralph 305
Tim 250
Barbara 85
Jane 160
Steve 180
Tom 210
Mike 165
Shirley 155
Pam 125
Frank 130

-----Elevator Riders-----

Name Weight
Bob 250
Ralph 305
Tim 250
Barbara 85
Jane 160

Total Weight: 1020
Number of Elevator Riders: 5

-----Method 2 -----
Ascending order by Weight:

Name Weight

0
Barbara 85
Pam 125
Frank 130
Anne 150
Shirley 155
Jane 160
Mike 165
Steve 180
Tom 210
Tim 250
Bob 250

-----Elevator Riders-----

Name Weight
Barbara 85
Pam 125
Frank 130
Anne 150
Shirley 155
Jane 160
Mike 165
Steve 180

Total Weight: 940
Number of Elevator Riders: 8

-----Method 3 -----
Descending order by Weight:

Name Weight

Ralph 305
Tim 250
Bob 250
Tom 210
Steve 180
Mike 165
Jane 160
Shirley 155
Anne 150
Frank 130
Pam 125
Barbara 85

-----Elevator Riders-----

Name Weight
Tim 250
Bob 250
Tom 210
Steve 180

Total Weight: 1030
Number of Elevator Riders: 4

The best Method was

Coded by

--------------------------------
Process exited after 0.6481 seconds with return value 0
Press any key to continue . . .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CA --Method 1 Name Weight ator Desktop\Chegg) C Ctest.exe 25e Ralph 250 Tim Barbara 85 Dane Steve 180 Tom Mike 165 Shirley 15

CA ator Desktop\Chegg) C Ctest.exe Method 2 Ascending order by Weight: Name Weight Barbara 85 Pam Frank 13e 125 15e Shirley 1

CN ator Desktop\Chegg) C Ctest.exe Total Weight: 94e Number of Elevator Riders: 8 Method 3 Descending order by Weight Name We

there is no error in code it display everything properly you can see i have change the value of Ralph from 305 to 250 and it came into sorting list with method 2.

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
where is the error in my code? it wont print all positions in the array for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an ...

    I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an algorithm .. CSE 1311-Project 4 Part I: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows: 150 250 Anne Bob Ralph 305...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT