#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;
main()
{
printf("-----------------------------------------------------\n");
printf("\t\tTotal Score calculator\n");
printf("-----------------------------------------------------\n");
int counter, x, studentcount = 1,sum = 0 ;
x = printMenu(studentglobal);
for (counter = 0; counter < x; counter++)
{
studentcount = studentcount + counter;
printf("Student : %d \n", studentcount);
getCarryMark(cm1);
if (cm1 >= 0 && cm1 <= 50)
{
printf("right range!!\n");
}
else
{
printf("INVALID RANGE!!!\n");
}
printf("%.2f\n", cm1);
}
}
int printMenu(int nstudent)
{
printf("Enter no of student: ");
scanf("%d", &nstudent);
return(nstudent);
}
float getCarryMark(float carrymark)
{
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}
So actually when I enter 200, it shows INVALID RANGE!!!, but when I enter 20 it still shows INVALID RANGE!!!. It somehow skipped the if statement. Please don't bother the other part, if I have any mistake tell me please. ert gf dfg dgd dg dfgd gd dg dg dgdfg
You need to return carrymark from getCarryMark:
float getCarryMark(float carrymark)
{
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}
You are missing a return statement in getCarryMarks method !
You missed the return statement in getCarryMark
getCarryMark function takes a parameter by value, modifies the value and returns it back, however, the returned value is never used. Modifying the parameter's value does not reflect this change to the outside since it has been passed by value.
I have partially updated the code so that it could execute the if statement. Please try the following code.
#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;
main()
{
printf("-----------------------------------------------------\n");
printf("\t\tTotal Score calculator\n");
printf("-----------------------------------------------------\n");
int counter, x, studentcount = 1,sum = 0 ;
x = printMenu(studentglobal);
for (counter = 0; counter < x; counter++)
{
studentcount = studentcount + counter;
printf("Student : %d \n", studentcount);
cm1 = getCarryMark();
if (cm1 >= 0 && cm1 <= 50)
{
printf("right range!!\n");
}
else
{
printf("INVALID RANGE!!!\n");
}
printf("%.2f\n", cm1);
}
}
int printMenu(int nstudent)
{
printf("Enter no of student: ");
scanf("%d", &nstudent);
return(nstudent);
}
float getCarryMark()
{
float carrymark = 0.0;
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}
Related
I was recently given an assignment by one of my professors in which I have to write a piece of code in which it prompts for a salary, years of service, and then based on those two pieces of info it calculates a bonus. I've worked with functions declared as doubles, but this is my first time working with void functions. I'm having trouble understanding how I can get my first function to save the prompted values for years of service and salary and then use those in the next function to calculate the bonus. This is what I currently have:
#include <cstdio>
void GetInput()
{
double salary;
int years_service;
printf("Enter your salary: ");
scanf("%lf", &salary);
printf("How many years have your served for us? ");
scanf("%d", &years_service);
}
void CalcRaise()
{
//I initialized salary and years_service because they would not compile
//otherwise. As expected, it does run but since they are set to 0, the
//bonus will be zero.
double salary = 0;
int years_service = 0;
double bonusA;
double bonusB;
double bonusC;
bonusA = salary * .02;
bonusB = salary * .05;
bonusC = salary * .10;
if ( years_service < 2)
{
printf("Here is your bonus: %lf", bonusA);
}
else if ( years_service > 5 && years_service < 10)
{
printf("Here is your bonus: %lf", bonusB);
}
else
{
printf("Here is your bonus: %lf", bonusC);
}
return;
}
int main()
{
GetInput();
CalcRaise();
return 0;
}
As I mentioned, I'm just having trouble figuring out how to save the values from my first function and use those to calculate the bonus. Any help is appreciated. -Thanks
Make all the variables global, and initialize those variables in the starting stage itself.
#include <stdio.h>
#include <stdlib.h>
double salary = 0;
int years_service = 0;
double bonusA;
double bonusB;
double bonusC;
void GetInput()
{
printf("Enter your salary: ");
scanf("%lf", &salary);
printf("How many years have your served for us? ");
scanf("%d", &years_service);
}
void CalcRaise()
{
bonusA = salary * .02;
bonusB = salary * .05;
bonusC = salary * .10;
if (years_service < 2)
{
printf("Here is your bonus: %lf", bonusA);
}
else if (years_service > 5 && years_service < 10)
{
printf("Here is your bonus: %lf", bonusB);
}
else
{
printf("Here is your bonus: %lf", bonusC);
}
}
int main()
{
GetInput();
CalcRaise();
return 0;
}
I am writing a program in C that plays the game Craps. After my first roll, when I either land the point (win) or lose, the program just ends instead of calling is_win or is_loss.
But if it calls is_win or is_loss on my first roll, everything works fine.
I'm fairly certain this has something to do with the pointer pbal. I've been getting errors in the debugger saying unable to read memory, also Exception Thrown: Read access violation. I am still fairly new to pointers so I'm assuming this is the root cause of the problem.
# include "header.h"
int print_menu(void) {
int choice = 0;
printf("\n1. Enter balance\n2. Play game\n3. Get rules\n4. End game");
printf("\nEnter which you would like to do: ");
scanf("%d", &choice);
return choice;
}
void print_rules(void) {
printf("\nFirst Roll: If your die roll adds to 7 or 11, you win.\nIf it adds to 2, 3, or 12, you immediately lose.\nIf it adds to another number, that number becomes your 'point'");
printf("\nSecond Roll: If your die roll adds to your point, you win. If it adds to 7, you lose.\nKeep rolling until you get one of these.\n\n");
}
int get_balance(balance) {
printf("\nEnter how much money you would like to add (whole dollars): ");
scanf("%d", &balance);
return balance;
}
int prompt_bet(balance) {
int bet = 0;
do {
printf("\nEnter how much you would like to bet for this game: ");
scanf("%d", &bet);
} while (bet > balance); //repeats until its false that bet>bal
return bet;
}
int roll_dice(void) {
srand((unsigned int)time(NULL));
int enter = 1;
printf("\nEnter a 0 to roll your dice: ");
scanf("%d", &enter);
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int dice = die1 + die2;
printf("You rolled a %d and a %d, with a total of %d.\n", die1, die2, dice);
return dice;
}
int calc_first_roll(int dice, int bet, int balance, int * pbal) {
int result0 = 0;
if (dice == 7 || dice == 11) {
is_win(bet, balance, pbal);
}
else if (dice == 2 || dice == 3 || dice == 12) {
is_loss(bet, balance, pbal);
}
else {
printf("Your point is %d", dice);
int point = dice;
int done = 1;
do {
dice = roll_dice();
done = calc_other_rolls(point, dice, balance, bet, *pbal);
} while (!done);
}
return result0;
}
void is_win(int bet, int balance, int *pbal) {
/* the pointer *pbal is pointing to mainbalance. I had to pass it
through everything to get it to affect mainbal the way I wanted it to.
Think of mainbalance as the permanent memory for keeping their bets & money right,
and int balance is just a messenger that doesn't get in the way of me trying
to use a pointer on mainbalance. */
*pbal = balance + bet;
printf("You win! Your new balance is %u\n", *pbal);
}
void is_loss(int bet, int balance, int *pbal) {
*pbal = balance - bet;
printf("You lost. Your new balance is %u\n", *pbal);
}
int calc_other_rolls(int point, int dice, int balance, int bet, int *pbal) {
int done = 0;
if (dice == 7) { //Goes straight to is_l / is_w instead of looping back to calc_first
is_loss(bet, balance, *pbal);
done = 0;
}
else if (dice == point) {
is_win(bet, balance, *pbal);
done = 0;
}
else {
done = 0;
}
return done;
}
# include "header.h"
int main(void) {
int mainbalance = 0;
int choice = 0;
int *pbal = &mainbalance;
do {
choice = print_menu();
if (choice == 1) {
mainbalance = get_balance();
printf("Your balance is: %d\n", mainbalance);
choice = 8; //reprints menu
}
if (choice == 3) {
print_rules();
choice = 8;
}
if (choice == 4)
exit(1);
if (choice == 2) {
int bet = prompt_bet(mainbalance);
int dice = roll_dice();
int x = calc_first_roll(dice, bet, mainbalance, pbal);
choice = 8;
}
} while (choice > 4 || choice < 1); //keeps running code until test becomes false.
return 0;
}
In this section:
if (dice == 7) { //Goes straight to is_l / is_w instead of looping back to calc_first
is_loss(bet, balance, *pbal);
done = 0;
}
else if (dice == point) {
is_win(bet, balance, *pbal);
done = 0;
}
You're not passing is_loss and is_win the pointer, you're passing the integer value, that pbal points to. * outside of a declaration is always dereferencing.
So if calc_other_rolls gets int *pbal as an argument and you wanna pass it to another function that takes int * then you should do func(pbal) and not func(*pbal), since the second one passes the value not the pointer.
Edit: As #hollow pointed out, this gets flagged by the compiler if you enable warnings, so use them.
I've made this program below to calculate the average mark of a student.
Everything works well until I use -1, it is supposed to stop the program as is display the average of all students that have been entered, say Goodbye! and then terminate.
I think my calculations might be wrong though because it is printing the wrong result for the average marks of the students.
Thanks in Advance.
#include <stdio.h>
int main(void)
{
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int exam_mark);
int i, a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark;
float average_mark = 0.0;
do
{
for (i = 0; i < 2; i++)
{
printf("Enter assignment 1 mark (-1 to quit): ");
scanf("%d", &a_mark1);
if(a_mark1 == -1)
{
average_mark += final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark);
if ((average_mark > 1 ) && (average_mark < 100 ))
{
printf("The average student mark is %.2f%% \n", average_mark);
}
printf("Goodbye! \n");
return 0;
}
printf("Enter assignment 2 mark: ");
scanf("%d", &a_mark2);
printf("Enter laboratory mark: ");
scanf("%d", &lab_mark);
printf("Enter quiz mark: ");
scanf("%d", &quiz_mark);
printf("Enter exam mark: ");
scanf("%d", &exam_mark);
printf("Student %d final mark: %.2f \n", i + 1, final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark));
}
}
while(a_mark1 != -1);
return 0;
}
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int exam_mark)
{
float final_mark = a_mark1 * 0.1 + a_mark2 * 0.15 + lab_mark * 0.15 + quiz_mark * 0.1 + exam_mark * 0.5;
return final_mark;
}
I think you need to rethink your logic a little bit. Why not use a while loop to control the flow. Then you can bail out of the program immediately if user inputs -1 right away. You should use an array to store the averages for each student, then you can loop through and find the class average as well.
-Your float final_mark function seems a little sketchy without any parenthesis.
-You should put your function prototype outside of main as well. See below changes.
#include <stdio.h>
#define MAX_STUDENTS 10 //define what the max number of students is
float final_mark(int mark1, int mark2, int lab, int quiz, int exam);
int main()
{
int i = 0, mark1 = 0, mark2 = 0,
lab = 0, quiz = 0, exam = 0;
int num_students;
float students_avg[MAX_STUDENTS] = {0}; //array to hold averages for students
float average = 0;
while (i < MAX_STUDENTS) {
printf("Enter assignment 1 mark (enter -1 to quit):\n");
scanf("%d", &mark1);
if (mark1 == -1)
break; //no more students, break out of while loop
printf("Enter assignment 2 mark: ");
scanf("%d", &mark2);
printf("Enter laboratory mark: ");
scanf("%d", &lab);
printf("Enter quiz mark: ");
scanf("%d", &quiz);
printf("Enter exam mark: ");
scanf("%d", &exam);
average = final_mark(mark1, mark2, lab, quiz, exam);
students_avg[i] = average; //add this average to array
printf("Student # %d average was %.2f\n", i, students_avg[i]);//debug info
i++;
}
num_students = i; //how many students grades did we read?
average = 0; //reset to 0 so we can use below
for (i = 0; i < num_students; i++)
average += students_avg[i];
if (num_students > 0)
printf("Class average is %.2f\n", average/num_students);
else
printf("Goodbye!\n");
return 0;
}
float final_mark(int mark1, int mark2, int lab, int quiz, int exam)
{
//we can just return the calculation
return ((mark1 * 0.1) + (mark2 * 0.15) + (lab * 0.15) + (quiz * 0.1) + (exam * 0.5));
}
Just erase two lines above printf("GOODBYE! \n"); you will get what you want.
maybe you have to initialized mark s variables to 0. and a_mark1 to 0 too if the user enter -1
I want to calculate the wages with struct and arrays but it is not working. You will understand the exercise better when you see the code. For some reason if in Wage and Days_Worked I enter a number(100 and 2)
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked; Won't give me 200 but something else. Generally the programm won't work and I am trying to find the reason.
#include <stdio.h>
struct User
{
char First_Name[25];
char Last_Name[25];
int Wage, Days_Worked;
int Tax;
int Wage_Booking;
int Net_Wage;
int Gross_Wage;
};
int main()
{
int i;
int Wage_Summary = 0;
struct User Wagered[1];
for(i = 0; i < 1; i++)
{
/*printf("First Name: ");
scanf("%s", &Wagered[i].First_Name);
printf("\n");
printf("Last Name: ");
scanf("%s", &Wagered[i].Last_Name);
printf("\n");*/
printf("Wage: ");
scanf("%d", &Wagered[i].Wage);
printf("\n");
printf("Days He Worked: ");
scanf("%d", &Wagered[i].Days_Worked);
printf("\n");
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked;
Wagered[i].Wage_Booking = Wagered[i].Gross_Wage * 0.2;
Wagered[i].Tax = (Wagered[i].Gross_Wage - Wagered[i].Wage_Booking) * 0.05;
Wagered[i].Net_Wage = Wagered[i].Gross_Wage - Wagered[i].Wage_Booking - Wagered[i].Tax;
Wage_Summary += Wagered[i].Net_Wage;
}
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
return 0;
}
This statement :
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
is outside your for loop. Therefore it will be executed when i has value 1, thus accessing Wagered[1].Gross_Wage, which does not exist Wagered[1] is out of your array's bounds. Move it inside your for loop, like this :
for(i = 0; i < 1; i++)
{
.
.
.
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked;
Wagered[i].Wage_Booking = Wagered[i].Gross_Wage * 0.2;
Wagered[i].Tax = (Wagered[i].Gross_Wage - Wagered[i].Wage_Booking) * 0.05;
Wagered[i].Net_Wage = Wagered[i].Gross_Wage - Wagered[i].Wage_Booking - Wagered[i].Tax;
Wage_Summary += Wagered[i].Net_Wage;
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
}
and you will see the correct result printed.
I am working on an assignment in C where I have to read in multiple people's heights and weights and determine their bmi. I then classify them into their respective bmi categories, but I am getting stuck on how to do this properly, this is my code thus far:
# include <stdio.h>
int main () {
int people;
double bmi, weight, inches;
printf("How many peoples? > ");
scanf("%d", &people);
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", people);
scanf("%lf %lf", &inches, &weight);
people--;
}
while (people > 0);
bmi = (weight / (inches * inches)) * 703;
if (bmi < 18.5) {
printf("Under weight: %d\n", people);
}
else if (bmi >= 18.5 && bmi < 25) {
printf("Normal weight: %d\n", people);
}
else if (bmi >= 25 && bmi < 30) {
printf("Over weight: %d\n", people);
}
else if (bmi >= 30) {
printf("Obese: %d\n", people);
}
return 0;
}
where am i going wrong? where do i fix this code?
Use some data structure for storing data. You are getting input for more than one people but, finally processed for one person.
And also people--; is done. so people variable is decremented up to zero, which makes while to exit without executing your BMI calculation.
Modified Code:
#include <stdio.h>
#define MAX_PEOPLE 100
int main () {
int people;
double bmi[MAX_PEOPLE], weight[MAX_PEOPLE], inches[MAX_PEOPLE];
int index = 0;
printf("How many peoples? > ");
scanf("%d", &people);
index = people;
do {
printf("Enter height (inches) and weight (lbs) (%d left) > ", index);
scanf("%lf %lf", &inches[index], &weight[index]);
index--;
}while (index > 0);
for(index = 0; index < people; index++)
{
bmi[index] = (weight[index] / (inches[index] * inches[index])) * 703;
if (bmi[index] < 18.5) {
printf("Under weight: %d\n", index);
}
else if (bmi[index] >= 18.5 && bmi[index] < 25) {
printf("Normal weight: %d\n", index);
}
else if (bmi[index] >= 25 && bmi[index] < 30) {
printf("Over weight: %d\n", index);
}
else if (bmi[index] >= 30) {
printf("Obese: %d\n", index);
}
}
return 0;
}
Right now you are processing the same data.
Each time you assign a new value to weight the old one is erased.
You could create multiple variables like so:
double weight1, weight2, weight3, weight4, ...etc (highly unpractical!!)
or
create an array of doubles:
double weight[100];
and refer to each specific double variable like this:
scanf("%lf %lf", inches[0], weight[0]);
scanf("%lf %lf", inches[1], weight[1]);
scanf("%lf %lf", inches[2], weight[2]);
You see where i-m getting at ? you can manipulate the array tru a for loop.