Programming C final A
Given the variables isFullTimeStudent and age, write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true .
( age < 19 || isFullTimeStudent )
Given the integer variables yearsWithCompany and department, write an expression that evaluates to true if yearsWithCompany is less than 5 and department is not equal to 99.
( yearsWithCompany < 5 && department != 99)
Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has already been declared , use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.
do { printf ("*"); j ++; } while ( j < n );
Write the definition of a function powerTo, which receives two parameters . The first is a double and the second is an int . The function returns a double . If the second parameter is negative, the function returns 0. Otherwise it returns the value of the first parameter raised to the power of the second.
double powerTo(double y ,int x) { if ( x < 0) { return 0; } else { return pow(y,x); } }
Write a for loop that prints the integers 1 through 40, separated by spaces or new lines. You may use only one variable , count which has already been declared as an integer .
for (count = 1 ; count <= 40; count ++) { printf ("%d\n",count); }
Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 through 64, and adds 1 to the variable seniors if age is 65 or older.
if ( age < 18 ){ minors = minors + 1 ; }else if ( age > 17 && age < 65 ){ adults = adults + 1 ; }else{ seniors = seniors + 1; }
Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values . Declare any additional variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.
int m = firstPlaceWinner ; firstPlaceWinner = secondPlaceWinner; secondPlaceWinner = m ;
Write the definition of a function max that has three int parameters and returns the largest.
int max ( int1, int2 , int3 ) { int max = int1; if (int2 > int1) max = int2 ; if (int3 > max) max = int3 ; return max ; }
Write the definition of a function twice, which receives an integer parameters and returns an integer that is twice the value of the parameter .
int twice (int parameters) { parameters *= 2 ; return parameters; }
Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by spaces.
int x ; for (x = 6 ; x < 200; x+=6 ) { printf("%d\n",x); }
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.
int x ; for (x = 80; x>=20 ;x = x- 2) { printf ("%d\n", x); }
Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces.
int x ; for ( x = 11 ; x <= 121; x = x + 2) { printf("%d\n", x); }
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed.
int x = 0 ; while(x >= 0) { scanf ("%d", & x); if (x > 100 ) { printf ("%d\n" , x); } }
Write a statement that prints a double named cost
printf("%lf", cost);
Write an expression that attempts to read an integer from standard input and store it in an int variable , x, that has already been declared.
scanf("%d", & x);
Given that two int variables , total and amount, have been declared , write a sequence of statements that: initializes total to 0 reads three values into amount, one at a time. After each value is read in to amount, it is added to the value in total (that is, total is incremented by the value in amount).
total = 0 ; amount = 0 ; int i =0; for (i = 1; i <=3; i++) { scanf ("%d", & amount ); total += amount; }
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared , use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.
total = 0 ; for (k = 0 ; k<=n; k ++) { total = total + k*k*k; }
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared , use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.
total = 0 ; k = 0 ; while ( k <= n ) { total = total + k * k * k ; k ++ ; }
Given int variables k and total that have already been declared , use a while loop to compute the sum of the squares of the first 50 positive integers and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0; while (k <= 50 ) { total = total + k * k ; k ++; }
Write a statement that increments the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.
total = amount + total;
Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message to standard output depending on the value of its parameter . If the parameter equals 1, the function prints disagree If the parameter equals 2, the function prints no opinion If the parameter equals 3, the function prints agree In the case of other values , the function does nothing. Each message is printed on a line by itself.
void printAttitude ( int x ) { if ( x == 1 ) printf ("\n disagree \n"); else if ( x == 2 ) printf ("\n no opinion \n"); else if ( x == 3 ) printf ("\n agree \n"); else return ; }
Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has already been declared , use a while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.
while (j < n ) { printf ("*"); j ++ ; }