Commission Sales and Largest Number

advertisement
09/04/13
Week 2 Assignments
Table of Contents
Problem 3.19 (Commission Sales) – Page 2
Pseudocode – Page 3
Flowchart – Page 4
Code – Page 5
Problem 3.24 (Largest Number) – Page 7
Pseudocode – Page 8
Flowchart – Page 9
Code – Page 10
1|Page
Daniel Sharpe
09/04/13
Week 2 Assignments
Daniel Sharpe
Problem:
One large chemical company pays its salespeople on a commission basis. The salespeople receive
$200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000
worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. Develop a program
that will input each salesperson’s gross sales for last week and will calculate and display that
salesperson's earnings. Process one salesperson's figures at a time. Here is a sample input/output
dialog:
Enter sales in dollars (-1 to end): 5000.00
Salary is: $650.00
Enter sales in dollars (-1 to end): 1234.56
Salary is: $311.11
Enter sales in dollars (-1 to end): 1088.89
Salary is: $298.00
Enter sales in dollars (-1 to end): -1
2|Page
09/04/13
Week 2 Assignments
Daniel Sharpe
Pseudocode
Input sales from each week to calculate commission and add $200 dollars to that commission. When
sentinel value is entered employee’s earnings are displayed.
Initialize Integers
Initialize counter
Initialize salary
Initialize commission
Initialize total
Input earnings
While sentinel is not yet entered
Calculate 5% of sales for total commission and add $200
Add to counter
Total and display
Ask to input next sale
If sentinel (-1) is entered
Display earnings
Else no salary entered before sentinel entered
Display No Salary Entered
3|Page
09/04/13
Week 2 Assignments
Flowchart
4|Page
Daniel Sharpe
09/04/13
Week 2 Assignments
Daniel Sharpe
Program Code
#include <stdio.h>
int main (void)
{
unsigned int counter;
int sales;
float total;
float commission;
//float to get decimal
//float to get decimal
sales = 0;
total = 0;
commission = 0;
counter = 0;
//set sales to zero
//set total to zero
//set commission to zero
//set counter to zero
printf("Welcome to SalesForce Sales and Commission Tracker\n");
Welcome//
printf("\n");
//Extra Space for Readability//
printf("Please input your earnings for the week:\n");
input of sales//
printf("(Enter -1 to complete and total your earnings)\n");
//Instructions on how to total using sentinal//
scanf("%d",&sales);
//User Inputs Sales//
while( sales > 0)
//Generic
//Asks for
{ //any number less than zero will trigger total
commission = (sales * 0.09 + 200); //calculate commission of sales * 9% + $200
counter = counter + 1;
total = total + commission;
//count and add one to counter
//take total and add commission
printf("\n"); //Space for readability
printf("Your take home (commission + $200 is $%.2f)\n" , commission);
//Display earnings for sales
printf("\n"); //Space for readability
printf("Please input your earnings for the week:\n");
//Asks for
input of sales//
printf("(Enter -1 to complete and total your earnings)\n");
//Instructions on how to total using sentinal//
scanf("%d", &sales);
//Visual Studio warned me to use scan_f, so I turned off warnings
}
if ( counter != 0) { //Terminate and total as long as counter is more than 0
total = total + commission;
printf("\n"); //Space for readability
5|Page
09/04/13
Week 2 Assignments
printf("Total Sales Plus Commission is $%.2f\n" , total);
commission to 2 decimal places
printf("\n"); //Space for readability
}
else {
because counter = 0
Daniel Sharpe
//Total sales and
//If no sales are entered when sentinal is entered
printf("No sales entered\n"); //Display if sentinal entered immediately
}
}
6|Page
09/04/13
Week 2 Assignments
Daniel Sharpe
Problem:
The process of finding the largest number (i.e., the maximum of a group of numbers) is
used frequently in computer applications. For example, a program that determines the
winner of a sales contest would input the number of units sold by each salesperson.
The salesperson who sold the most units wins the contest. Write a pseudocode
program and then a program that inputs a series of 10 numbers and determines and
prints the largest of the numbers.
Hint: Your program should use three variables as follows:
counter: A counter to count to 10 (i.e., to keep track of how many numbers have been
input and to determine when all 10 numbers have been processed)
number: The current number input to the program
largest: The largest number found so far
7|Page
09/04/13
Week 2 Assignments
Daniel Sharpe
Pseudocode
Input the number of units sold by 10 salespeople. When the counter reaches ten, find the largest
number of units sold and display.
Initialize Variables
Units to 0
Greatest to 0
Counter to 0
While counter is less than 10
Input units sold
If units entered is greater than greatest variable
Change greatest variable to equal units
Else (units is less than greatest variable)
Keep greatest variable = greatest variable
If units entered is less than 0 (negative number)
Print “Can’t enter negative numbers, program will end.”
Return 0 and end program
Add one to the counter
Print greatest number
8|Page
09/04/13
Week 2 Assignments
Daniel Sharpe
Flowchart
**Could really use some feedback on this Professor Keary**
What do I do with so many nested statements? Especially the whole if (units < 0) to take care of negative
numbers? Do I need to put those statements in my flowchart? Thanks for the feedback.
9|Page
09/04/13
Week 2 Assignments
Daniel Sharpe
Code
#include <stdio.h>
int main(void)
{
int units = 0;
int greatest = 0;
int counter = 1;
// initialize units sold and set at 0
// initialize greatest units sold and set at 0
// initialize counter to 1
printf("Enter the number of units each sales person sold\n"); // intro
printf("to find who sold the most (up to ten sales).\n");
printf("\n");
while ( counter <= 10 ) {
// loop until counter hits 10
printf("Enter your units sold:"); // display text to user to enter units
sold
scanf("%d" , &units);
// user inputs units sold
if (units > greatest) {
// determines if units enters is bigger
than greatest variable
greatest = units;
// if units is greater, greater variable
changed to units
}
else {
//if units not greater than
greatest then...
greatest = greatest; //greatest stays greatest
}
if (units < 0) {
// check to see if negative number
entered
printf("Negative Numbers Cannot be Entered.\n"); // tell user negative
numbers cannot be enetered
printf("Program will terminate.\n");
return 0;
// end program if negative
numbered entered
}
counter = counter + 1;
//add one to counter
}
printf("Greatest number of units sold is %d\n" , greatest);
entered is greatest
}
10 | P a g e
//print which number
Download