Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background Information: - you’ll make a simple program in which you can analyze

ID: 3631631 • Letter: B

Question

Background Information:

- you’ll make a simple program in which you can analyze if you met your budget goals at the end of the month or not. This program will further extend your practice with arrays, control statements, and will provide you with an opportunity to work with floating point type variables.

Programming Problem Description:
In this program the user is going to be prompted to enter in what his or her budgeted amount was for the month in three different basic spending categories and then the program will begin to take expenses for each category one at a time.
The three categories will be nourishment, transportation, and entertainment. The program should be able to handle up to 20 individual expenses for each category.
The user should not be forced to enter in all 20 expenses for each category, so inform the user that by entering -1 for an expense that input for that category will be suspended. Once the user has entered in all of the expenses for the three budgeted categories, the program should output a table that informs the user of the budget, the used portion of the budget, and the remaining portion of the budget for each category. The table should also include the same information summed up.


Programming Problem Instructions:
1. Use the template given below to find code that can be used for the program.
2. First inform, prompt, and receive from the user the budgeted amount for each category.

3. Next inform, prompt, and receive from the user the expenses for the first category, and then repeat the same for the next two categories. If a user inputs -1 then that value should not be stored in the array and the input for that category should cease. Also, note that the user should only be able to input at a maximum 20 expenses. In order to accomplish this step you must create a function called inputExpenses that takes at least one argument which is a floating point array to store information for that category (passed by reference).

4. Output a table which includes information in the following form. You don’t have to follow this format exactly, but the table should be neat and readable and mostly aligned.
Category Budget Used Remaining
Nourishment $xxx $xxx $xxx
Transportation $xxx $xxx $xxx
Entertainment $xxx $xxx $xxx
Total $xxx $xxx $xxx

• See the template for some function calls and other ideas to help you

Grading & Learning Guidelines:
Comments:

• You will lose points if you do not include a thorough program description
• Include comments throughout your program

Structure:
• Do not create or use any pointers in this lab
• Organization and neatness of written code will be considered
Solution:

• The output for this program ought to be neat and ought to include all that
the instructions ask for. It is suggested to use the templates printf calls
• All the functionality from the instructions should be included
Error Free:
• Must not be able to access elements outside of the array size.
• It is not required to ensure the user is only inputting integers or floating
point numbers, or even positive numbers.

TEMPLATE:

//Step 1 Code

printf("Monthly Budget "); printf("Input budgeted amount for each spending category. ");    printf("Nourishment $ "); scanf("%d", ); //or %f   

printf("Transportation $ "); scanf("%d", ); //or%f     

printf("Entertainment $ "); scanf("%d", ); //or %f


//Step 2 Code

float inputExpenses ( float category[20] , const char categoryStr[] );
float inputExpenses ( float category[20] , const char categoryStr[] ) {
} //notice that a size is included in the parameter definition //the compiler will for the most part ignore this value //but, it tells us that we expect the array to contain 20 elements //and we will write code such that we modify only 20 elements
//categoryStr is a string that is passed to this function and printed with the input instructions
printf(" %s Input expenses for this category. Maximum input is 20 expenses. Input -1 if done. ", catStr );    printf( "%2d : " ); scanf ( "%f", );
    }
//Step 3 Code

printf( nResults "); printf( "Category Budget Used Remaining ");
//for each next printf the arguments are the following:

printf( "%s $%7.2f $%7.2f $%7.2f ", , , , ); //first argument is a string containing the name of the category

printf( "%s $%7.2f $%7.2f $%7.2f ", , , , ); //second argument is a float representing the budget for that category

printf( "%s $%7.2f $%7.2f $%7.2f ", , , , ); //third argument is a float representing the total expenses printf( "%s $%7.2f $%7.2f $%7.2f ", , , , ); //fourth argument is a float representing the remaining (budget - used)

here is a sample of what the output will look like (minus the first line)



Explanation / Answer

I used the template and it retty much guided the whole program. First step is to write your main funtion for execution as any other program.

The variables declared were all floats (my choice, you could opt for integers) , 3 budget variables to hold the total budget per category, 3 expenses 20-item arrays (also of type float) to hold up to 20 expenses recorded for the month, and 3 float variables to hold the total of all 20 expenses for each category. Then to do our results computation, we just subtract the total expenses from the budget for each category, and for the total, we just sum all 3 budgets and subtract the sum of amm 3 expenses totals from it to get the total remainder.

According to the template, the Step 2 should happen using a function, inputExpenses, you give it the expenses array for a category and that category's name, and it handles user input - stores it in the array until the user either reaches 20 expenses or enters -1, then the "return" value of inputExpenses is a float, so we know what it's hinting at : sum the expenses you got from the user in the array, and return them from this function - so this function takes the array and the category name, interactys with the user, and spits out the total expenses for that category :) in that case we can just assign what it spits out to the corresponding expensesTotal float variable.

Once we do that for all 3 categories, we're at Step 3 where we just display what we have according to the template, we use the budget values and the expenses totals to display the results. and voila :)

Notice that I used which is the tab character to get text to align nicely for output display :) his is a trial and error process until you get your stuff aligned the way you want.

I hope this is what you needed -if so then please rate! If you still have questions about this you can PM me or post a comment :)

#include <stdio.h>

/* Function prototype declaration */
float inputExpenses ( float category[20] , const char categoryStr[] );

int main()
{
/* Variables declaration */

/* 3 category budget variables */
float nourishBudget, transportBudget, entertainBudget;

/* 20-element expenses per category variables */
float nourishExp[20], transportExp[20], entertainExp[20];

/* total expenses per category results */
float nourishTot, trasportTot, entertainTot;

/* Helper variables - for loops and such */
int i;

/* Initializing array variables */
for (i = 0; i < 20; i++)
{
  nourishExp[i] = 0;
  transportExp[i] = 0;
  entertainExp[i] = 0;
}

/* Step 1 - prompting user for 3 category budgets */

printf("Monthly Budget ");
printf("Input budgeted amount for each spending category. ");
printf("Nourishment $ ");
scanf("%f", &nourishBudget);
printf("Transportation $ ");
scanf("%f", &transportBudget );
printf("Entertainment $ ");
scanf("%f", &entertainBudget);

/* Step 2 - prompting user for Expenses */

nourishTot = inputExpenses(nourishExp, "Nourishment");
trasportTot = inputExpenses(transportExp, "Transportation");
entertainTot = inputExpenses(entertainExp, "Entertainment");

/* Step 3 - printing the results */

printf(" Results ");
printf( "Category Budget Used Remaining ");
printf( "%s $%7.2f $%7.2f $%7.2f ","Nourishment" ,nourishBudget ,nourishTot ,nourishBudget-nourishTot );
printf( "%s $%7.2f $%7.2f $%7.2f ","Transportation" ,transportBudget ,trasportTot ,transportBudget -trasportTot);
printf( "%s $%7.2f $%7.2f $%7.2f ","Entertainment" , entertainBudget, entertainTot, entertainBudget-entertainTot);
printf( "%s $%7.2f $%7.2f $%7.2f ", "Total",nourishBudget+transportBudget +entertainBudget, nourishTot+trasportTot+entertainTot,(nourishBudget+transportBudget +entertainBudget)-(nourishTot+trasportTot+entertainTot) );  

return 0;
}

/* Function Implementation - input expenses array and category name - return expenses total */
float inputExpenses ( float category[20] , const char categoryStr[] )
{
float input = 0;
float sum = 0;
int i = 0;
printf(" %s Input expenses for this category. Maximum input is 20 expenses. Input -1 if done. ", categoryStr );
do {
  printf( "%2d : ", i+1 );
  scanf ( "%f", &input);
  if(input != -1)
  {
   category[i] = input;
   i++;
  }
}while( (input!=-1) && (i < 20) );

for(i = 0; i < 20; i++)
  sum = sum + category[i];

return sum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote