I've seen coursera mentioned a few times here. What exactly is the format of those lessons? Seems you have to enroll over a certain duration? Is it essentially college courses made public? Do they grade stuff?
#include<stdio.h>
#include<ctype.h>
int sectioncode();
int studentnumber();
int main()
{
// Declare variables.
int seccode, code, stnum;
// Get section code.
seccode = sectioncode();
// Get Student Number
stnum = studentnumber();
// Get Marks
return 0;
}
int sectioncode()
{
int scode;
char c = '0';
do
{
printf("Enter the section code (1-4): ");
scanf("%d", &scode);
fflush(stdin); // Flush the buffer so that you don't get infinite looping when entering characters which don't fit the parameters anyway.
} while (scode < 1 || scode > 4);
return scode;
}
[B]int studentnumber()
{
long int stnum, divis, i = 0, zeros = 0;
do
{
printf("Enter student number: ");
scanf("%d",&stnum);
fflush(stdin);
}while(stnum < 1 || stnum > 999999999);
divis = stnum;
while (divis != 0)
{
divis /= 10;
++i;
}
i = 9-i;
while (zeros < i)
{
printf("0");
zeros++;
}
printf("%d", stnum);
return stnum;
}[/B]
I need help with C++. The header of a class I'm defining looks like this (I only kept the relevant parts if the following code):
Code:#ifndef TOOLS_HPP #define TOOLS_HPP #include <complex> using namespace std; class tools { public: }; #endif
Code:#include "tools.hpp" const complex<double> I(0, 1); const double PI = atan(1)*4;
#include<stdio.h>
#include<ctype.h>
int sectioncode();
int getstudentnumber();
float getmarks();
void printnum(int studentid);
float getsectionaverage();
int cleanbuffer();
int main()
{
// Declare variables.
int seccode, code, stnum;
float grades;
[B] // Get section code.
seccode = sectioncode();
// Get Student Number
stnum = getstudentnumber();
// Get Marks
grades = getmarks();
//Print Student Number
printnum(stnum);[/B]
}
Put `seccode = sectioncode();` before the while loop, and then again as the last statement inside of the loop.Code:#include<stdio.h> #include<ctype.h> int sectioncode(); int getstudentnumber(); float getmarks(); void printnum(int studentid); float getsectionaverage(); int cleanbuffer(); int main() { // Declare variables. int seccode, code, stnum; float grades; [B] // Get section code. seccode = sectioncode(); // Get Student Number stnum = getstudentnumber(); // Get Marks grades = getmarks(); //Print Student Number printnum(stnum);[/B] }
I'm trying to get the bolded to loop until I enter a 0 for seccode. Encapsulating it in a "while(seccode != 0)" ends the loop only after all of the functions execute. I want to terminate it right after entering 0 for seccode. Also, I can't use break or if statements...
Put `seccode = sectioncode();` before the while loop, and then again as the last statement inside of the loop.
I'll be taking this, too.
I really liked the previous course, and I didn't even know much about functional programing.
i = 0; i < (100 / grossPay); i++
Code:i = 0; i < (100 / grossPay); i++
As long as grossPay ≤ 100 at least. Counting from 0 to less than whatever that ends up being. Is that what you're trying to do?
Is it possible to create a for loop condition where the condition is that a number must be divisible by another number?
ie.
Code:(i = 0; 100 / grossPay; i++)
grosspay will always be at least 100 or greater. I want it so that the loop only runs if grosspay is perfectly divisible by 100. (ie 200, 300, 5000, etc.)
the condition should evaluate to true/false, and should represent a test for whether some number N is divisible by some number Q. The way to do that would be:
`N % Q == 0`
% is the modulo operator. It N % Q divides N by Q, but returns the remainder instead of the result. A remainder of zero means it's divisible.
the condition should evaluate to true/false, and should represent a test for whether some number N is divisible by some number Q. The way to do that would be:
`N % Q == 0`
% is the modulo operator. It N % Q divides N by Q, but returns the remainder instead of the result. A remainder of zero means it's divisible.
grosspay will always be at least 100 or greater. I want it so that the loop only runs if grosspay is perfectly divisible by 100. (ie 200, 300, 5000, etc.)
Pretty much, my function needs to return the deduction for long term benefits(LBT).LBT is charged at $2 per every full $100 of gross weekly earnings to a maximum LTB payment of $100 per week. An example of a gross weekly pay of $163 will result in a $2 LTB deduction, as there is only one full $100 amount.
This is C btw.
grosspay -= ((int)grosspay / 100) * 2;
You say you want to deduct $2 for every $100 in grosspay, why do you need a loop for this?
Just divide grosspay by 100 and deduct this many $2s from the grosspay...
Code:grosspay -= ((int)grosspay / 100) * 2;
Something like that should do the trick, typecasting the result of the division to an int will discard the values after the decimal point.
You may want to change the code so it is more elegant though =)
double longTermBenefits(double gross) {
int i;
int ltb = 0;
for(i = 0; 100 % gross == 0; i++) {
ltb + 2;
if (ltb > 100) {
break;
}
}
return ltb;
}
I can't change the value of the grosspay variable, as I'm using that value for a variety of other functions in my program.
double longTermBenefits(double gross) {
int i = gross / 100;
int ltb = i * 2;
return ltb;
}
Code:double longTermBenefits(double gross) { int i = gross / 100; int ltb = i * 2; return ltb; }
Is this what you want?
No, Dr_Swales' code works fine and will only give deductions for full 100s. The truncation that happens when assigning the floating point number into an integer turns the 1.63 into 1.That wouldn't work as int i doesn't take into account that gross must be perfectly divisible by 100 for the function to do its job properly. As it is right now that function would divide gross by 100 whether it be perfectly divisible or not, (ie gross = 163 would still be used by int i, which in turn would be used by int ltb, to give me 3.26) which would then be used by ltb to multiply it by 2 which I don't want. A $2 deduction should only be applied if the gross is perfectly divisible by 100.
That wouldn't work as int i doesn't take into account that gross must be perfectly divisible by 100 for the function to do its job properly. As it is right now that function would divide gross by 100 whether it be perfectly divisible or not, (ie gross = 163 would still be used by int i, which in turn would be used by int ltb, to give me 3.26) which would then be used by ltb to multiply it by 2 which I don't want. A $2 deduction should only be applied if the gross is perfectly divisible by 100.
Ah I misunderstood what you wanted.
I thought you wanted a $2 deduction for every $100. So $100 would be a $2 deduction and $134 would also be a $2 deduction. Where as $264 for example would be a $4 deduction.
Also, you misunderstood my code. Forcing the divide into an int will disregard the decimals. So when gross = 163, i would be 1 because 163/100=1.63, ignore the .63. Then ltb would be 1*2=2.
(Hard to type on a phone!)
EDIT: Thanks Water for explaining while I struggle to type on this.... thing!
ah, then that will work. Thanks. Guess I'll see if everything works in a couple of hours when I finish writing my code.
void outputSalaried(int id, double salary, [B]double gross[/B], double ltb, double ei, double cpp, [B]double pTax,double fTax,[/B] int year,int month,int day) {
double totalDeductions;
double netPay;
printf("Kars Payroll Systems\n");
printf("102 Orfus Road, Downsview ON\n");
prinf("\n");
printf("Employee Number %d ", id);
printf("For Week Ending: %d\n", payDate);
printf("\n");
printf("Salary Paid: %lf\n", annualSalary);
print("\n");
printf("GROSS PAY: %lf", gross);
printf("\n");
printf("DEDUCTIONS\n");
printf("Long Term Benefits: %lf", longTermBenefits(gross));
printf("Employment Insurance: %lf", employmentInsurance(gross));
printf("Canada Pension Plan: %lf", canadaPensionPlan(gross));
printf("Provincial Tax: %lf", &pTaxPointer);
printf("Federal Tax: %lf", &fTaxPointer);
printf("\n");
printf("TOTAL DEDUCTIONS: %lf", totalDeductions);
printf("\n");
printf("NET PAY: %lf", netPay);
double grossPaySalaried(double annualSalary) {
return annualSalary / (1/52);
}
double grossPayHourly(double rate, double numberOfHours) {
if (numberOfHours > 44) {
return (rate * 1.5) * numberOfHours;
}
else {
return rate * numberOfHours;
}
}
void taxes(double gross, double* fTaxPointer, double* pTaxPointer) {
if(gross % 20000 == 0) {
*fTaxPointer = gross * 0.16;
if(gross % 40000 == 0) {
*fTaxPointer = gross * 0.23;
if(gross % 60000 == 0) {
*fTaxPointer = gross * 0.29;
}
}
}
*pTaxPointer = *fTaxPointer * 0.47;
}
No, Dr_Swales' code works fine and will only give deductions for full 100s. The truncation that happens when assigning the floating point number into an integer turns the 1.63 into 1.
My second problem comes with fTax and pTax. My problem stems from the two arguments in outputSalaried coming from a single void function that doesn't return anything. I have no idea how to extract the local variables from the function and pass them as arguments in the outputSalaried function. Here is the function that calculated provincial(pTax) and federal(fTax) taxes.
Code:void taxes(double gross, double* fTaxPointer, double* pTaxPointer) { if(gross % 20000 == 0) { *fTaxPointer = gross * 0.16; if(gross % 40000 == 0) { *fTaxPointer = gross * 0.23; if(gross % 60000 == 0) { *fTaxPointer = gross * 0.29; } } } *pTaxPointer = *fTaxPointer * 0.47; }
pretty much what I'm supposed to do in this function is to code the body of the function so that it passes back the federal and provincial income tax deductions through the pointers fTaxPointer and pTaxPointer. The tax is calculated as follows:
16% on the first $20,000 earned annually
23% on the next $20,000 earned annually
29% on the remainder earned annually
The Provincial income tax which is collected along with the Federal Tax, is an additional 47% of the Federal tax collected.
Of course I have no idea if my code is even remotely correct or not.
bool test_taxes()
{
double pTax = -1;
double fTax = -1;
double gross1 = 0;
double expected1 = 0;
double gross2 = 5000;
double expected2 = 0.16 * 5000;
double gross3 = 25000;
double expected3 = 0.16 * 20000 + 0.23 * 5000;
double gross4 = 45000;
double expected4 = 0.16 * 20000 + 0.23 * 20000 + 0.26 * 5000;
taxes(gross1, &fTax, &pTax);
if(expected1 != fTax)
return false;
taxes(gross2, &fTax, &pTax);
if(expected2 != fTax)
return false;
taxes(gross3, &fTax, &pTax);
if(expected3 != fTax)
return false;
taxes(gross4, &fTax, &pTax);
if(expected4 != fTax)
return false;
}
main
output 'test_taxes'
bool result = test_taxes()
output result
Interview on Tuesday for an internship. Really nervous. Wish me luck!
... but readability really helps maintainability down the line. Especially if someone else works with the code at some point.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
FILE *fp;
int int1, int2;
void
endprog(void *p)
{
char *q = (char *) p;
printf("Goodbye from %s\n", q);
}
void *
reader(void * p)
{
char *n = "Reader Thread";
pthread_cleanup_push(endprog, (void *)n);
sigset_t set;
int sig = SIGUSR1;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
while(1)
{
sigwait(&set, &sig);
printf ("Thread 1 submitting : " ,"%d %d \n", int1, int2);
}
phthread_cleanup_pop(1);
return ((void *)NULL);
}
void *
calculator(void * p)
{
char *n = "Calculator Thread";
pthread_cleanup_push(endprog,(void*) n);
sigset_t set;
int sig = SIGUSR1;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
while(1)
{
sigwait(&set, &sig);
printf ("Thread 2 calculated : ","%d \n", int1 + int2);
}
phthread_cleanup_pop(1);
return ((void *)NULL);
}
int
main(int argc, char *argv[])
{
fp= fopen(argv[1], "r");
if(fp = NULL)
{
printf("File doesn't exist\n");
}
pthread_t a;
pthread_t t;
char *s = "c";
sigset(SIGUSR1,reader);
sigset(SIGUSR1,calculator);
pthread_create(&a, NULL, calculator, (void *)s);
pthread_create(&t, NULL, reader, (void *)s);
sleep(1);
while(1)
{
while(fscanf(fp, "%d %d", &int1, &int2)!= EOF)
{
}
}
pthread_cancel(a);
pthread_cancel(t);
pthread_join(a,NULL);
pthread_join(t,NULL);
fclose(fp);
return (0);
}
I'll take issue number two, since the first issue doesn't seem quite as clear.
Your code is wrong.
- You call the function with a parameter gross. If gross == 20,001 the program doesn't enter the first if statement. It would have to be exactly 20,000 to get modulo 0. (20001 % 20000 == 1).
- Then you also have to look at the nested ifs. Your gross doesn't change. Gross can't be first 20,000 and then 40,000.
- The two parameters pTax and fTax are output parameters. Because a function can only return one value (here void) this function uses two parameters as outputs (they are pointers, so you can change them from within the function, whereas gross isn't a pointer and you can't change it).
- And then, you don't really calculate the taxes. Lets say someone wants to know the taxes for 45,000. The way this works is:
1. pay 16% for 20,000.
2. pay 23% for 20,000.
3. pay 26% for 5,000.
How about you try that test-driven. I recommend that you write a test for taxes. It will fail. And then you work on taxes till the test is passed.
If you call taxes with gross == 0, gross == 5000, gross == 25000, and gross == 45000, does the function return 0, 0.16 * 5000, 0.16 * 20000 + 0.23 * 5000, and 0.16 * 20000 + 0.23 * 20000 + 0.26 * 5000, respectively?
You can write another function test_taxes somewhere, in which you call taxes with the gross values above and then compare if the output matches.
And then you work on taxes, till the test passes. (You might want to change test_taxes (e.g. with an additional output to tell you where exactly it failed, or you can even split up test_taxes into four tests (test0gross_taxes, test5000gross_taxes, ...; you could extract a function from test_taxes that takes both gross and the expected value and returns true or false so the code gets less repetitive) and then do the output in your main.
Then you can really start from the bottom and get it to work for the easiest case first and build up from there.
For now, I'll leave it to you to figure out the actual function. But as soon as you have a test up and running it gets a lot easier.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
void taxes(double gross, double* fTaxPointer, double* pTaxPointer) {
if(gross >= 20000) {
printf("printing first set\n");
*fTaxPointer = gross * 0.16;
printf("ftax is %lf\n", fTaxPointer);
if(gross >= 40000) {
printf("printing second set\n");
*fTaxPointer = gross * 0.23;
printf("ftax is %lf\n", fTaxPointer);
if(gross >= 60000) {
printf("printing third set\n");
*fTaxPointer = gross * 0.29;
printf("ftax is %lf\n", fTaxPointer);
}
}
}
printf("printing ptax\n");
*pTaxPointer = *fTaxPointer * 0.47;
printf("ptax is %lf", pTaxPointer);
}
int main(void)
{
double gross = 65000;
double ftax = 0;
double ptax = 0;
printf("test taxes\n");
taxes(gross, &ftax, &ptax);
return 0;
}
Any reason as to why this is the case?
of course, it must be obvious by now that pointers are my worst enemy.
You forgot to dereference your pointers, fTaxPointer and pTaxPointer should be *fTaxPointer and *pTaxPointer. The way it is at the moment you are just outputting the address stored by the pointer.
Have you been given these function prototypes and you have to fill in the function body?
http://cslibrary.stanford.edu/102/btw, is there some type of reference I can access/buy that goes more indepth with pointers and explains how they work. I thought taking the C class would help, but I'm just as confused with pointers as when I was trying to teach myself them in C++.
of course, it must be obvious by now that pointers are my worst enemy.
void taxes(double gross, double* fTaxPointer, double* pTaxPointer) {
//using 45000 as an argument for gross
if(gross >= 20000) {
printf("printing first set\n");
*fTaxPointer = 20000 * 0.16;
printf("ftax is %lf\n", *fTaxPointer); // ftax = 3200
if(gross >= 40000) {
printf("printing second set\n");
*fTaxPointer += 20000 * 0.23; // ftax = 7800
printf("ftax is %lf\n", *fTaxPointer);
if(gross > 40000) {
printf("printing third set\n");
*fTaxPointer += (gross - 40000) * 0.29; // ftax = 9250
printf("ftax is %lf\n", *fTaxPointer);
}
}
}
printf("printing ptax\n");
*pTaxPointer = *fTaxPointer * 0.47; //ptax = 4347.5
printf("ptax is %lf\n", *pTaxPointer);
}
int main(void)
{
double ftax = 0;
double ptax = 0;
printf("test taxes\n");
taxes(45000, &ftax, &ptax);
return 0;
}
Ok, I think I have it down now after re-reading what I need to do. Using 45000 as the argument for gross, it looks like I'm getitng the correct values I should be getting, unless I'm reading what I need to do wrong again :/
Code:void taxes(double gross, double* fTaxPointer, double* pTaxPointer) { //using 45000 as an argument for gross if(gross >= 20000) { printf("printing first set\n"); *fTaxPointer = 20000 * 0.16; printf("ftax is %lf\n", *fTaxPointer); // ftax = 3200 if(gross >= 40000) { printf("printing second set\n"); *fTaxPointer += 20000 * 0.23; // ftax = 7800 printf("ftax is %lf\n", *fTaxPointer); if(gross > 40000) { printf("printing third set\n"); *fTaxPointer += (gross - 40000) * 0.29; // ftax = 9250 printf("ftax is %lf\n", *fTaxPointer); } } } printf("printing ptax\n"); *pTaxPointer = *fTaxPointer * 0.47; //ptax = 4347.5 printf("ptax is %lf\n", *pTaxPointer); } int main(void) { double ftax = 0; double ptax = 0; printf("test taxes\n"); taxes(45000, &ftax, &ptax); return 0; }
bool test_taxes(gross, expected)
{
double pTax = -1;
double fTax = -1;
taxes(gross, &fTax, &pTax);
return (expected == fTax)
}
Getting better. However, so far you only really cover gross values over 40,000. Eg. if you input 0 everything goes horribly wrong. Or if you input 26,000, you use the right tax rate for the first 20,000, but don't consider the remaining 6,000 for the higher tax rate.
Try using the test code I posted at first. I think it may be a pure-C issue that you can only have one return statement. In this case it doesn't much matter.
Code:bool test_taxes(gross, expected) { double pTax = -1; double fTax = -1; taxes(gross, &fTax, &pTax); return (expected == fTax) }
And then you can run the specific tests in your main:
- test_taxes(0, 0)
- test_taxes(5000, 0.16 * 5000)
- test_taxes(25000, 0.16 * 20000 + 0.23 * 5000)
- test_taxes(45000, 0.16 * 20000 + 0.23 * 20000 + 0.26 * 5000)
For each test you can output if it returns true or false.
//tax function
void taxes(double gross, double* fTaxPointer, double* pTaxPointer) {
if(gross >= 20000) {
*fTaxPointer = 20000 * 0.16;
if(gross >= 40000) {
*fTaxPointer += 20000 * 0.23;
if(gross > 40000) {
*fTaxPointer += (gross - 40000) * 0.29;
}
}
}
*pTaxPointer = *fTaxPointer * 0.47;
}
//output function
void outputHourly(int id, double rate, double hours, double gross, double ltb, double ei,double cpp, double pTax,double fTax,int year,int month,int day) {
double totalDeductions;
double netPay;
printf("Chidi Payroll Systems\n");
printf("102 Orfus Road, Downsview ON\n");
printf("\n");
printf("Employee Number %d ", id);
printf("For Week Ending: %d/%d/%d\n", year, month, day);
printf("\n");
printf("Hourly Rate: %0.2lf ", rate);
printf("Hours Worked: %0.2lf\n", hours);
printf("\n");
printf("GROSS PAY: %0.2lf\n", gross);
printf("\n");
printf("DEDUCTIONS:\n");
printf("Long Term Benefits: %0.2lf\n", ltb);
printf("Employment Insurance: %0.2lf\n", ei);
printf("Canada Pension Plan: %0.2lf\n", cpp);
printf("Provincial Tax: %0.2lf\n", pTax);
printf("Federal Tax: %0.2lf\n", fTax);
printf("\n");
printf("TOTAL DEDUCTIONS: %0.2lf\n", totalDeductions);
printf("\n");
printf("NET PAY: %0.2lf\n", netPay);
}
//calling tax function and output function
double fTaxH = 0;
double pTaxH = 0;
taxes(hGross, &fTaxH, &pTaxH);
printf("ftax is %lf", fTaxH); //test to make sure ftax/ptax output there correct values
printf("pTax is %lf", pTaxH); //currently both pTax and fTax evaluate to 0.0000
outputHourly(id, rate, hours, hGross, ltbH, eiH, cppH, pTaxH, fTaxH, year, month, day);
Ah, I think you may be getting the requirements messed up.
At this point I'm almost ocmpletely done, but it seems that I've run into one final snag. All my functions output perfectly except my taxes function. For some reason if I pass the variable 'gross' as an argument(the variable gross equals to grossPayHourly function, which returns the gross value) pTax and fTax output 0.00000 instead of the correct value. However if I pass a random number, say 70000 as an arguments instead, fTax and pTax both output their correct values. Any idea why this is happening?
Its normal to have trouble getting started. It requires a completely different way of thinking than most people are used to.46% on my C assignment is the second failing mark I've received so far. I should probably just drop programming...
Its normal to have trouble getting started. It requires a completely different way of thinking than most people are used to.
It's quite possible to fail miserably the first few times you try but still eventually find success. Not that you necessarily will, but failing a few assignments (or even a course) isn't something to despair over.
Introductory programming courses tend to have very high failure rates.
46% on my C assignment is the second failing mark I've received so far. I should probably just drop programming...
Ugh does anyone here know a good tutorial for implementing an RSS reader into android project?