CIS3355: Data Structures

The University of Texas at El Paso

Dr. Peeter Kirs

 

C/C++ Programming: Sample Problems and Exercises

 

NOTE: It doesn’t do you any good to look at the answers before you attempt to answer the questions first

 

005.0000 Describe the History of C and C++.

 

      SEE SOLUTION

 

006.0000 What are some of the basic programming conventions in C/C++ ?

 

      SEE SOLUTION

 

006.0010       What are the reserved words in C? In C++?

 

      SEE SOLUTION

                 

006.0120 What are Precompiler Instructions and what do they do? What are some examples?

 

      SEE SOLUTION

 

006.0140 What is the main function and what does it do? What are some examples of usage?

 

      SEE SOLUTION

 

006.0160 What are user defined functions and what do they do? What are some examples of usage?

 

      SEE SOLUTION

 

010.0000  What are the Standard/Common numeric Operators in C/C++ language?

 

      SEE SOLUTION

 

010.0005  What are the Standard/Common relational Operators in C/C++ language?

 

      SEE SOLUTION

 

010.0010  What are the order of Operators?

 

      SEE SOLUTION

 

010.0020  What are the Binary operators in C/C++ and what do they do?

 

      SEE SOLUTION

 

010.0040  What are the assignment operators in C/C++ and what do they do?

 

      SEE SOLUTION

 

010.0060  What are the unary operators in C/C++ and what do they do?

 

      SEE SOLUTION

 

010.0062       What are parentheses used for and how are they used?

 

      SEE SOLUTION

 

010.0065       What is the difference between dividing integers and dividing floats? Why?

 

      SEE SOLUTION

 

010.0070  Explain the difference between  prefix notation incrementing/decrementing and postfix notation incrementing/decrementing

 

      SEE SOLUTION

 

010.0200  Distinguish between the two relational operators &&(And) and ||(Or)

 

      SEE SOLUTION

 

010.0500  Why are there no printing facilities in C/C++? How do we print in C and C++?

 

      SEE SOLUTION

 

010.0520       What are some of the printf specifiers in C? What do they do?

 

      SEE SOLUTION

 

010.0540       If I declare something as the data type char (character) how is it that I am able to misuse a print specifier (i.e., print it out as a short, an integer, a long, or a float)?

 

      SEE SOLUTION

 

010.0560 Suppose that a section of RAM appeared as:           

                 

 4058

4059

4060

4061

10010010

11001011

01011001

00010010

 

I issued the commands:       long longvar;

                                                printf(“%lu”, &longvar);

 

And I received the output:     4058

 

What would I get as output if I entered the command:

 

                  printf(“%ld  %lu  %d  %c”, longvar, longvar, longvar, longvar);

 

(Note: Let’s assume that integers are still 16-bits long, so %d interprets accordingly)

                                            

011.0000 What are ellipses { .. } do, why are they important, and what rules must we follow?

 

012.0000  What is another name for the Branching Statement and what does it do?

 

      SEE SOLUTION

 

013.0000 What is a for statement used for, what are its components and what do they do?

 

013.0100 How might I use a for statement to print out the values of 21 through 210??

 

013.0500 What is an infinite loop and what is an example of one in a for statement?

 

014.0000 What is a while statement used for and how do they work?

 

014.0050 How might I use a while statement to print out only the even values of the integers beween 1 and 100?

 

014.0100 What is the difference between a while statement and a do .. while statement? Which is generally preferred?

 

014.0130 How might I use a do .. while statement to print out the octal and hexadecimal values of the integers beween 1 and 500?

 

014.0150 Can infinite loops take place using either a while statement or a do .. while statement? What are some examples?

 

014.1000 What are the differences between a for statement and a while statement? Is one preferred over the other?

 

020.0000 What are user defined functions, what do they do, what are the benefits of using they, and how are they called and used?

 

020.0050 What are they three major components of every function, and why is each one necessary?

 

020.0100 What is a function prototype, what does it do, and what happens if it is not included?

 

020.0150 What does the return statement do? Is it necessary?

 

020.0300 What are the benefits of using functions?

 

020.0350 What are the differences between local and global variables? Is one preferred over the other? Why?

 

020.0370 Why is it that I can use the same variable name in two different functions?

 

020.1000 How might I write a program to print the squares of all of the even integers between 1 and 100 and all of cubes of the odd integers between 1 and 100 using functions?

 

020.1005 Are there different functions that could be written to produce the same output as above? What are some of them?

 

020.1010 If I used global variables instead of local variables, how different would the functions used above be?

 

100.0100 Given the following C/C++ program:

 

#include <iostream.h>   // The standard I/O header file

 

void main()

{

      char  clocation1 = 'D',  clocation2 = 10;

      int   ilocation1 = '3',  ilocation2 = 5;

      float flocation1 = 7.5,  flocation2 = 5.0;

 

      cout << clocation1 + clocation1/clocation2 * clocation2;

      cout << endl;

      cout << ilocation1++/ilocation2 + ilocation1 - ilocation2 * 3 + 8;

      cout << endl;

      cout << ilocation1 + ilocation1 * 2 + ++ilocation2 % 2 - 4;

      cout << endl;

      cout << flocation1 + flocation1 / flocation2 * 2;

      cout << endl;

}

 

A. What would be displayed by the line:

 

            cout << clocation1 + clocation1/clocation2 * clocation2;

 

      SEE SOLUTION

 

B. What would be displayed by the line:

 

            cout << ilocation1++/ilocation2 + ilocation1 - ilocation2 * 3 + 8;

 

      SEE SOLUTION

 

C. What would be displayed by the line:

 

            cout << ilocation1 + ilocation1 * 2 + ++ilocation2 % 2 - 4;

 

      SEE SOLUTION

 

D.  What would be displayed by the line:

 

            cout << flocation1 + flocation1 / flocation2 * 2;

                                                                                                

      SEE SOLUTION

 

100.0110 A quick note before we begin this question. The following program will run in C/C++. However, the output might seem a little strange, at first. If a logical statement evaluates to ‘TRUE”, a “1” will be printed. If it evaluates to “FALSE” a “0” will be printed out. Keep this in mind when answering the following questions.

 

Given the following C++ program:

 

#include <iostream.h>

 

void main()

{

            short a = 1, b = 2, c = 3, d = 4, e = 5;

           

            cout << ((a > b) || (e > d));

            cout << endl;

            cout << ((a > b) && (e > d));

            cout << endl;

            cout << ((b == 2) && (d > 4) || (e == 4) || (c > b));

            cout << endl;

            cout << ((b + 1 >= c) || (d / 2 == 0) && (e % 3 == 0));

            cout << endl;

            cout << ((a + b > d) && (c++ * b > b + e) || (e - d == a));

            cout << endl;

            cout << ((++a >= b) && (c - a == b) || (c % b == 0));

            cout << endl;

}

 

A. What would be printed out by the statement: 

 

cout << ((a > b) || (e > d));

                                                                                                

      SEE SOLUTION

 

B. What would be printed out by the statement: 

 

cout << ((a > b) && (e > d));

                                                                                                

      SEE SOLUTION

 

C. What would be printed out by the statement:

 

cout << ((b == 2) && (d > 4) || (e == 4) || (c > b));

                                                                                                

      SEE SOLUTION

 

D. What would be printed out by the statement: 

 

cout << ((b + 1 >= c) || (d / 2 == 0) && (e % 3 == 0));

                                                                                                

      SEE SOLUTION

 

E. What would be printed out by the statement: 

 

cout << ((a + b > d) && (c++ * b > b + e) || (e - d == a));

                                                                                                

      SEE SOLUTION

 

F. What would be printed out by the statement:

 

cout << ((++a >= b) && (c - a == b) || (c % b == 0));

                                                                                                

      SEE SOLUTION

 

100.0120. Given the following C program, show the output that would be produced

 

#include <stdio.h>

int main()

{

      int  a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, r1, r2, r3, r4, r7;  

      float v = 0.5,  x = 1.0,  y = 2.0,  z = 3.0,  r5,  r6;

 

      r1 = a + e/(e - b);

      printf("Output A = %d\n",r1);                // Output A = SEE SOLUTION

      r2 = -b - (c + d) % b;

      printf("Output B = %d\n",r1);                // Output B = SEE SOLUTION

      r3 = a-- * b - ++c;

      printf("Output C = %d\n",r3);               // Output C = SEE SOLUTION

      r4 = c - a + e/b;  

      printf("Output D = %d\n",r4);               // Output D = SEE SOLUTION

      r5 = z/y + (++g % d);

      printf("Output E = %f\n",r5);                 // Output E =  SEE SOLUTION

      r6 = z - (float) b * z/y;

      printf("Output F = %f\n",r6);                 // Output F = SEE SOLUTION

      r7 = (int) (x + y) - f;

      printf("Output G = %d\n",r7);               // Output G = SEE SOLUTION

      if ((b < d) && (h % 2 == 1))

            printf("Output H = TRUE\n");  

      else                                                        // Output H = SEE SOLUTION

     printf("Output H = FALSE\n");  

      if ((((int) (z/x)) == 3) || ((float) h != 8.0))     

            printf("Output I = TRUE\n");  

      else                                                           // Output I = SEE SOLUTION

            printf("Output I = FALSE\n");

}