Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


APSC 143 - Fall 2021

Introduction to Computer Programming for Engineers

Practice Problems - Weeks 9 & 10


Practice Problems

What is the decimal number represented by 110101?

Given the following code:

int x = 10;

int n = 2++x ;

what are the values of x and n after the last line of code?

Given 2 int type variables, x and y, where x = 5, y = 4. What does the following expression evaluate to: x == 5||x < 5&&y! = 6&&y >= 5

Given a char type variable, coin, use the conditional operator to print ”Heads” if coin is equal to ’h’, and print ”Tails” if coin is equal to ’t’.

Let’s make similar code using an if-else statement, only this time if coin is neither ’h’ or ’t’ print ”Coin not flipped correctly”.

Make code with the same functionality as the previous question, only using a switch- case statement.

Take your switch case statement from the previous question and remove all break statements. What is the output if coin == ’t’.

Given an int type variable myInt, write a while loop that will add 1 to myInt and then prints its value until myInt is greater than 10.

Obtain the functionality from the previous question using a do-while loop instead.

What is printed by each of the previous programs if myInt is decalred with a value of 11.

Write a program that will continuously get user input for a int type number and adds the value to a variable called sum, until the user enters 0. At that point, print out the value of sum.

Declare an array, called count, that can store 5 int type values and initialize all elements to 0, in one line of code.

Using a repetition control structure, assign the elements of count to be 1, 2, 3, 4, and 5. Do not manually code in the size of the array in your structure, instead use the sizeof function.

Declare and initialize an integer array, called count2D, with 2 rows and 3 columns where the values in the rows are 1, 2, 3, and 4, 5, 6, respectively.

Given the array in the previous question write a repetition control structure that can print out the elements of the array. Do not manually code in the size(s) of the array in your structure, instead use the sizeof function.

Declare and initialize a string with the name myString and the text ”Hello” by initializing one character at a time.

Declare and initialize a string with the name myString and the text ”World” by initializing the entire text at once.

Combine the strings from the previous question to obtain a string that equals ”Hello World”.

Assuming we use the fgets function to get user input for a string called userString. Write some code to check if there is a newline character at the end of the string and alter the value of the string variable so that it can be printed correctly with printf.

Declare an array of 2 strings called groceries. Then on a separate line (or lines) of code initialize the strings to be ”milk” and ”eggs”, in that order.

Let’s say we wanted to add ”bread” onto the end of the array from the previous question. Create a new array, newList, and accomplish this functionality.

Let’s instead achieve the functionality of the previous 2 questions using memory allocation and reallocation. This will allow us not to need the newList variable.

Write code to print out the strings stored in the memory block allocated in the previous question.

Create a program that sorts an array of strings with the following values: ”Oxygen”, ”Nitrogen”, ”Hydrogen”, ”Helium”.

After sorting the array, search the array for the element ”Oxygen” and print the index it is at.

Given an int array, called intArray, containing 10 values create an array of pointers to store the addresses of the values in that int array, and store the addresses in the array.

Use the array of pointers from the previous question to change the values of the int array to be the values 1-10.

Given a file, ”examPractice.txt” that contains 100 int type values on separate lines, where the file is in the same location as your executable file, open the file and read in the values into an array. Be sure to check if the file was opened successfully.

Create a recursive function to copy a string, stringA, into another string, stringB.

Create a recursive function get the sum of all int values between 0 and a number given by the user.


Written Solutions

53

x=11 and n=22

1

My code:

( coin==’h ) ? printf (”Heads\n”) : printf (” Tails \n” ) ;

My code:

i f ( coin==’h )

printf (”Heads\n” ) ;

else i f ( coin==’ t )

printf (” Tails \n” ) ;

else

printf (”Coin not flipped correctly \n” ) ;

My code:

switch( coin ){

case ’h :

printf (”Heads\n” ) ;

break ;

case t :

printf (” Tails \n” ) ;

break ;

default :

printf (”Coin not flipped correctly \n” ) ;

break ;

}

Tails

Coin not flipped correctly

My code:

while(myInt<=10){

myInt++;

printf (”%d” , myInt ) ;

}

My code:

do{

myInt++;

printf (”%d” , myInt ) ;

}while(myInt <=10);

nothing printed, and 12

My code:

int int

myInt ; sum=0;

while (1){

scanf (”%d” , &myInt ) ;

i f (myInt==0)

break ;

sum += myInt ;

}

printf (”%d” , myFloat ) ;

My code:

int myArray [ ] = {0 ,0 ,0 ,0 ,0};

There are many ways to do this, but here is my code:

for ( int i =0; i< sizeof (myArray)/ sizeof (myArray [ 0 ] ) ; i++){ count [ i ] = i +1;

}

My code:

int count2D [ 2 ] [ 3 ] = {{1 ,2 ,3} , {4 ,5 ,6}};