Cs 1361

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
Ok guys so I've been in CS 1361 for about two months now. I thought I'd make this so I could post some of my code and get some feedback.

Lab 25

https://drive.google.com/file/d/0BzD0FjUel2BqdjZCZVFCRnZLbkU/edit?usp=sharing

Code:
// Josh
// CS 1361
// Lab 25
// Chips and Salsa Program

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

using namespace std;

#define ID "Josh - CS 1361 - Lab 24"

int getLowest(int[], int);
int getHighest(int[], int);
int getTotal(int[], int);

int main()
{
	
	const int ARRAY_SIZE = 5;
	string salsa[ARRAY_SIZE] = { "Mild  ", "Medium", "Sweet ", "Hot   ", "Zesty " };
	int sales[ARRAY_SIZE];
	
	cout << ID << endl << endl;	
		
	for (int count = 0; count < ARRAY_SIZE; count++)
	{	
		cout << "Jars sold last month of " << salsa[count] << ": ";
		cin >> sales[count];

		while (sales[count] < 0)
		{
			cout << "Please enter a valid input for jars sold : ";
			cin >> sales[count];
		}
	}

	getLowest(sales, ARRAY_SIZE);
	getHighest(sales, ARRAY_SIZE);
	getTotal(sales, ARRAY_SIZE);

	
	int highest = getHighest(sales, ARRAY_SIZE);
	int lowest = getLowest(sales, ARRAY_SIZE);
	int total = getTotal(sales, ARRAY_SIZE);

	cout << endl << endl;

	cout << "       Salsa Sales Report         " << endl << endl;
	cout << "Name                     Jars Sold" << endl;
	cout << "__________________________________" << endl;

	for (int count = 0; count < ARRAY_SIZE; count++)
	{
		cout << salsa[count] << setw(28) << sales[count] << endl;
	}

	cout << endl;

	cout << "Total Sales: " << setw(21) << total << endl;
	cout << "High Seller: " << salsa[highest] << endl;
	cout << "Low Seller : "  << salsa[lowest] << endl;
	
	

	return 0;
}

int getLowest(int sales[], int size)
{
	int lowest;

	lowest = sales[0];
	for (int count = 1; count < size; count++)
	{
		if (sales[count] < lowest)
			lowest = count;
	}

	return lowest;
}

int getHighest(int sales[], int size)
{
	int highest;

	highest = sales[0];
	for (int count = 1; count < size; count++)
	{
		if (sales[count] > highest)
			highest = count;
	}

	return highest;
}

int getTotal(int sales[], int size)
{
	int total = 0;

	for (int count = 0; count < size; count++)
	{
		total += sales[count];
	}

	return total;
}

I can't figure out how to get it to display the correct "highest" and "lowest". Any ideas?
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Fist quick comment

Case 1
the Error message is incorrect as related to the test condition
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
I don't know if it just got lost in the google doc, but please for the love of all that is good and right in the world...indent!
 

serpretetsky

Senior member
Jan 7, 2012
642
26
101
I noticed you included <string> and <iomanip>. Do you use either one of them?

Also, indents are funky like someone else mentioned. I don't know if this is google screwing up or not.
First file is missing indents completely
Second file appears to have almost random indents.

You should indent a block of code (the stuff enclose in { } ) with a single indentation. Once you leave that block of code you should remove that indentation.

I guess you could argue you should indent after the cases as well, but keep it consistent.

Code:
switch (i_choice)
{               
        case 1: cout << "Enter the circle's radius: ";
                cin >> d_radius;
                                                
                if (d_radius<=0)
                {
                         cout << endl << "The radius can not be less than zero." << endl << endl;
                         return 0;
                }
 // ...
}
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
First note, you are switching on magic numbers, I would avoid that. Rather, use a constant or an enum. This will clear up the switch statement.

Next, this is one big function, I would advise against that. Rather split things up. Make a function to get the required parameters for a rectangle, triangle, and circle. Splitting this into several methods will make things much more readable.

Another note, don't leave in commented out code. You really should be using some sort of source control (git is easy to setup, even for a single user).

Now for the code bugs, for those interested, here is a "not poorly formatted" version of the code

Code:
// CS 1361
// Lab 14
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

#define ID "CS 1361 - Lab 14"

const double D_PI = 3.14159;

int main()
{
	int i_choice;
	double d_radius;
	double d_length;
	double d_width;
	double d_base;
	double d_height;

	cout << ID << endl << endl;

	cout << "Geometry Calculater" << endl;
	cout << "\t1. Calculate the Area of a Circle" << endl;
	cout << "\t2. Calculate the Area of a Rectangle" << endl;
	cout << "\t3. Calculate the Area of a Triangle" << endl;
	cout << "\t4. Quit" << endl << endl;

	cout << "Enter your choice (1, 2, 3, or 4): ";
	cin >> i_choice;

	cout << endl;

	switch (i_choice)
	{
		case 1:
			cout << "Enter the circle's radius: ";
			cin >> d_radius;
			if (d_radius<=0)
			{
				cout << endl << "The radius can not be less than zero." << endl << endl;
				return 0;
			}
			else 
				cout << endl;
			cout << "The area is: " << D_PI*pow(d_radius, 2) << endl << endl;
			break;
		case 2:
			cout << "Enter the rectangle's length: ";
			cin >> d_length;
			{
				if (d_length<=0)
					cout << endl << "The length can not be less than zero." << endl << endl;
				return 0;
				
				cout << endl << "Enter the rectangle's width: ";
				cin >> d_width;
				if (d_width<=0)
				{
					cout << endl << "The width can not be less than zero." << endl << endl;
					return 0;
				}
				else 
					cout << endl;
				cout << "The area is: " << d_length*d_width << endl << endl;
				break;
			}
		case 3:
			cout << "Enter the triangle's base: ";
			cin >> d_base;
			if (d_base<=0)
				cout << endl << "The base can not be less than zero." << endl << endl;
			return 0;
			cout << endl << "Enter the triangle's height: ";
			cin >> d_height;
			if (d_height<=0)
			{
				cout << endl << "The height can not be less than zero." << endl << endl;
				return 0;
			}
			else 
				cout << endl;
			cout << "The area is: " << d_base*d_height*.5 << endl << endl;
			break;
		case 4:
			cout << "Program ending." << endl;
		default:
			cout << "The valid choices are 1 through 4.  Run the" << endl;
			cout << "program again and select one of those." << endl << endl;
	}
	return 0;
}

Now, what you should notice is that there are several unreachable code locations. For example, if someone enters "2", the program will exit silently because the return following the length check is not scoped. There are issues like this all over the place. They become plainly obvious with properly formatted code.
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
Thanks for the input guys. I went back through after uploading the code and realized I had a lot of errors within it. I didn't bother uploading the updated version since I had to submit it to be graded.

We are starting loops now so I may upload another piece of code later.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Paste the code snippets into the post box/window and surround it with the code tags.
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
While code

Code:
// CS 1361
// Lab 15
// This lab lets the user choose what to find the area of either a circle, rectangle, or triangle.


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

#define ID "CS 1361 - Lab 14"

const double D_PI = 3.14159;


int main()

{

        //Variables
        int i_choice = 0;
        double d_radius;
        double d_length;
        double d_width;
        double d_base;
        double d_height;

        //ID Line
        cout << ID << endl << endl;

        while (i_choice!=4)
        {

        //List
        cout << "Geometry Calculater" << endl << endl;
        cout << "\t1. Calculate the Area of a Circle" << endl;
        cout << "\t2. Calculate the Area of a Rectangle" << endl;
        cout << "\t3. Calculate the Area of a Triangle" << endl;
        cout << "\t4. Quit" << endl << endl;

        //Stream Manipulators
        cout << fixed << setprecision(4) << showpoint <<endl;

        //Choice Input
        cout << "\tEnter your choice (1, 2, 3, or 4): ";
        cin >> i_choice;

        cout << endl;

        switch (i_choice)
                {
                
                //Choice 1
                case 1: cout << "\tEnter the circle's radius: ";
                                        cin >> d_radius;

                                        while (d_radius<=0)
                                        {
                                                cout << "\tThe radius can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the circle's radius: ";
                                                cin >> d_radius;
                                        }
                                                cout << endl << "\tThe area is: " << D_PI*pow(d_radius, 2) << endl << endl;
                                        
                                        break;

                case 2: cout << "\tEnter the rectangle's length: ";
                                        cin >> d_length;

                                        while (d_length<=0)
                                        {
                                                cout << "\tThe length can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the rectangle's length: ";
                                                cin >> d_length;
                                        }
                                        
                                        cout << endl << "\tEnter the rectangle's width: ";
                                        cin >> d_width;
                                        
                                        while (d_width<=0)
                                        {
                                                cout << "\tThe width can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the rectangle's width: ";
                                                cin >> d_width;
                                        }

                                                cout << endl << "\tThe area is: " << d_length*d_width << endl << endl;

                                        break;

                //Choice 3
                case 3: cout << "\tEnter the triangle's base: ";
                                        cin >> d_base;

                                        while (d_base<=0)
                                        {
                                                cout << "\tThe base can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the triangle's base: ";
                                                cin >> d_base;
                                        }

                                cout << endl << "\tEnter the triangle's height: ";
                                        cin >> d_height;

                                        while (d_height<=0)
                                        {
                                                cout << "\tThe heigth can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the triangle's height: ";
                                                cin >> d_height;
                                        }

                                                cout << endl << "\tThe area is: " << d_base*d_height << endl << endl;

                                        break;

                case 4: cout << "Program ending." << endl << endl;
                                break;

                //Default
                default: cout << "\tThe valid choices are 1 through 4.  Run the" << endl;
                                 cout << "\tprogram again and select one of those." << endl << endl;
                }
        
                        
        } 

        return 0;

}

do-while
Code:
// CS 1361
// Lab 16
// This lab lets the user choose what to find the area of either a circle, rectangle, or triangle.  It contains a do-while loop.


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

#define ID "CS 1361 - Lab 16"

const double D_PI = 3.14159;


int main()

{

        //Variables
        int i_choice = 0;
        double d_radius;
        double d_length;
        double d_width;
        double d_base;
        double d_height;

        //ID Line
        cout << ID << endl << endl;

        do
        {

        //List
        cout << "Geometry Calculater" << endl << endl;
        cout << "\t1. Calculate the Area of a Circle" << endl;
        cout << "\t2. Calculate the Area of a Rectangle" << endl;
        cout << "\t3. Calculate the Area of a Triangle" << endl;
        cout << "\t4. Quit" << endl << endl;

        //Stream Manipulators
        cout << fixed << setprecision(4) << showpoint <<endl;

        //Choice Input
        cout << "\tEnter your choice (1, 2, 3, or 4): ";
        cin >> i_choice;

        cout << endl;

        switch (i_choice)
                {
                
                //Choice 1
                case 1: cout << "\tEnter the circle's radius: ";
                                        cin >> d_radius;

                                        while (d_radius<=0)
                                        {
                                                cout << "\tThe radius can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the circle's radius: ";
                                                cin >> d_radius;
                                        }
                                                cout << endl << "\tThe area is: " << D_PI*pow(d_radius, 2) << endl << endl;
                                        
                                        break;

                case 2: cout << "\tEnter the rectangle's length: ";
                                        cin >> d_length;

                                        while (d_length<=0)
                                        {
                                                cout << "\tThe length can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the rectangle's length: ";
                                                cin >> d_length;
                                        }
                                        
                                        cout << endl << "\tEnter the rectangle's width: ";
                                        cin >> d_width;
                                        
                                        while (d_width<=0)
                                        {
                                                cout << "\tThe width can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the rectangle's width: ";
                                                cin >> d_width;
                                        }

                                                cout << endl << "\tThe area is: " << d_length*d_width << endl << endl;

                                        break;

                //Choice 3
                case 3: cout << "\tEnter the triangle's base: ";
                                        cin >> d_base;

                                        while (d_base<=0)
                                        {
                                                cout << "\tThe base can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the triangle's base: ";
                                                cin >> d_base;
                                        }

                                cout << endl << "\tEnter the triangle's height: ";
                                        cin >> d_height;

                                        while (d_height<=0)
                                        {
                                                cout << "\tThe heigth can not be less than zero." << endl << endl;
                                                cout << "\tPlease re-enter the triangle's height: ";
                                                cin >> d_height;
                                        }

                                                cout << endl << "\tThe area is: " << d_base*d_height << endl << endl;

                                        break;

                case 4: cout << "Program ending." << endl << endl;
                                break;

                //Default
                default: cout << "\tThe valid choices are 1 through 4.  Run the" << endl;
                                 cout << "\tprogram again and select one of those." << endl << endl;
                }
        
                        
        } while (i_choice!=4);

        return 0;

}
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
#15

Typo: #define ID "CS 1361 - Lab 14"

Typo: "Geometry Calculater"

Style nitpick: have you learned about do ... while or just while .... ? If so, do you understand the difference? (If not, ignore this.)

Text nitpick: are these instructions correct? Try typing "5" and see what happens.

default: cout << "\tThe valid choices are 1 through 4. Run the" << endl;
cout << "\tprogram again and select one of those." << endl << endl;
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
#15

Typo: #define ID "CS 1361 - Lab 14"

Typo: "Geometry Calculater"

Style nitpick: have you learned about do ... while or just while .... ? If so, do you understand the difference? (If not, ignore this.)

Text nitpick: are these instructions correct? Try typing "5" and see what happens.

default: cout << "\tThe valid choices are 1 through 4. Run the" << endl;
cout << "\tprogram again and select one of those." << endl << endl;

The professor said do-while loops are really good for lists like the one in this lab. That's pretty much it.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
That was a hint then. Does your textbook explain the difference and when one is more appropriate than the other?
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
That was a hint then. Does your textbook explain the difference and when one is more appropriate than the other?

I haven't checked. The professor initially wanted us to write it first using a While statement. He then told us that the next lab would require us to substitute the While statement with a Do-While. That's why I have two things of code in my post.

Does that answer your question?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Yep, thanks. You'll see that the Do-While makes the logic a little cleaner, so if you'd already covered it then it would be a better choice. Since he wants you to learn them one at a time you should leave it as-is for now.
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
Ok guys, I need some advice. I'm trying to create a function that takes an array of numbers and sorts them from least to greatest. I THINK I'm very close to having it done because I can run the program and it sorts some of them lol.

If someone could take a look at my reorderArray function and give me a little nudge in the right direction I'd appreciate it.

The professor had his setup with a Do-While loop and a bool variable but I couldn't get mine to work so I changed over to a nested For loop.

The code is in the original thread btw.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
What algorithm are you supposed to be following? Bubble sort?

Also, aren't you listing your results before the sort has finished "bubbling" (moving) numbers?
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
Ok this is my last lab. I'm pretty sure I'm close but I not all the way there. I can't get the lowest and highest to show up correctly. Any ideas?
 

MarkLuvsCS

Senior member
Jun 13, 2004
740
0
76
I see a potential problem with the code.

Code:
int getLowest(int sales[], int size)
{
	int lowest;

	lowest = sales[0];
	for (int count = 1; count < size; count++)
	{
		if (sales[count] < lowest)
			[u]lowest = count;[/u]
	}

	return lowest;
}

You initially set the lowest to the value stored in the sales[0] location, but when you iterate you are going to set the lowest to your counter value. If the value stored in sales[0] when you start the iteration is greater than your ARRAY_SIZE then it would never change. If you just set it to lowest/higest = sales[count] your problem should clear up. Then on the final cout statement's you can just use the term 'lowest' 'highest' for those values since your function will return the int of those.
 
Last edited:

Fallen Kell

Diamond Member
Oct 9, 1999
6,230
543
126
My guess is that he hasn't learned bubble sort and is intended to use the min/max that he learned earlier and simply loop over the array until it is in order.
 

Josh123

Diamond Member
Aug 4, 2002
3,030
2
76
I see a potential problem with the code.

Code:
int getLowest(int sales[], int size)
{
	int lowest;

	lowest = sales[0];
	for (int count = 1; count < size; count++)
	{
		if (sales[count] < lowest)
			[u]lowest = count;[/u]
	}

	return lowest;
}

You initially set the lowest to the value stored in the sales[0] location, but when you iterate you are going to set the lowest to your counter value. If the value stored in sales[0] when you start the iteration is greater than your ARRAY_SIZE then it would never change. If you just set it to lowest/higest = sales[count] your problem should clear up. Then on the final cout statement's you can just use the term 'lowest' 'highest' for those values since your function will return the int of those.

But will I be able to display the salsa type that was highest/lowest rather than the sales number? This is the thing I'm running into. I can display the sales just fine but the lab calls for the salsa type to be displayed.

We used a bubble sort in the previous lab, would it be a better to use in this situation too?