C++ Repetition Worksheet

In Class Activity – Repetition Worksheet

  1. State what output, if any, results in each of the following statements.

 

  1. for(i = 1; i <= 1; i++)

cout<<“*”;

 

 

 

 

 

  1. for(i = 2; i >= 1; i++)

cout<<“*”;

 

 

 

 

 

  1. for(i = 1; i <= 1; i–)

cout<<“*”;

 

 

 

 

 

  1. for(i = 12; i >= 9; i–)

cout<<“*”;

 

 

 

 

 

  1. for(i = 0; i <= 5; i++)

cout<<“*”;

 

 

 

 

 

  1. for(i = 1; i <= 5; i++)

{

cout<<“*”;

i = i+1;

}

 

 

 

 

In Class Activity – Repetition Worksheet

 

  1. How many times will each of the following loops execute? What is the output in each case?

 

  1. x = 5;  y = 50;

do

x = x + 10;

while(x < y);

cout<<x<<” “<<y;

 

 

 

 

 

 

  1. x = 5;  y = 80;

do

x = x * 2;

while(x < y);

cout<<x<<” “<<y;

 

 

 

 

  1. x = 5;  y = 20;

do

x = x + 2;

while(x >= y);

cout<<x<<” “<<y;

 

 

 

 

 

 

  1. x = 5;  y = 35;

while(x < y)

x = x + 10;

cout<<x<<” “<<y;

 

 

 

 

 

  1. x = 5;  y = 30;

while(x <= y)

x = x * 2;

cout<<x<<” “<<y;

In Class Activity – Repetition Worksheet

 

  1. x = 5;  y = 30;

while(x > y)

x = x + 2;

cout<<x<<” “<<y;

 

  1. To learn how nested for loops work, do a walk-through of the following program segments and determine,

in each case, the exact output.

 

  1. int i,j;

for(i = 1; i <= 5; i++)

{

for(j = 1; j <= 5; j++)

cout<<setw(3)<< i*j;

cout<<endl;

}

 

 

 

 

 

 

 

 

 

  1. int i,j;

for(i = 1; i <= 5; i++)

{

for(j = 1; j <= 5; j++)

cout<<setw(3)<<i;

cout<<endl;

}

 

 

 

 

 

 

 

 

  1. int i,j;

for(i = 1; i <= 5; i++)

{

for(j = (i + 1); j <= 5; j++)

cout<<setw(5)<<j;

cout<<endl;

}

 

In Class Activity – Repetition Worksheet

 

  1. int i,j;

for(i = 1; i <= 5; i++)

{

for(j = 1; j <= i; j++)

cout<<setw(3)<<j;

cout<<endl;

}

 

 

 

 

 

 

 

 

  1.   const int m = 10;

const int n = 10;

int i,j;

for(i = 1; i <= m; i++)

{

for(j = 1; j <= n; j++)

cout<<setw(3)<<m*(i-1)+j;

cout<<endl;

}

 

 

 

 

 

 

 

 

  1. int i,j;

for(i = 1; i <= 9; i++)

{

for(j = 1; j <= (9 – i); j++)

cout<<” “;

for(j = 1; j <= i; j++)

cout<<setw(1)<<j;

for(j = (i -1); j >= 1; j–)

cout<<setw(1)<<j;

cout<<endl;

}

 

 

 

 

 

Due:    _________________________________

 

In Class Activity – Functions Worksheet

 

  1. What is the output of the following C++ program? (Note that the function sqrt returns the square root of

its argument. For example, sqrt(16.0) = 4.0. The specification of the function sqrt is in the header file math.h)

 

#include <iostream>

#include <cmath>

 

int main()

{

int counter;

 

for(counter = 1; counter <= 100; counter++)

if(pow(floor(sqrt(counter)),2) == counter)

cout<<counter<<” “;

cout<<endl;

return 0;

}

 

  1. Which of the following function headings are valid? If they are invalid, explain why.

 

  1. one(int a, int b)

 

  1. int thisone(char x)

 

  1. char another(int a, b)

 

  1. double yetanother

 

  1. Consider the following statements :

double num1, num2, num3;

int int1, int2, int3;

int value;

 

num1 = 5.0; num2 = 6.0; num3 = 3.0;

int1 = 4; int2 = 7; int3 = 8;

and the function prototype

 

double cube(double a, double b, double c);

 

Which of the following statements are valid? If they are invalid, explain why.

 

  1. value = cube (num1, 15.0, num3);

 

  1. cout<<cube(num1, num3, num2);

 

  1. cout<<cube(6.0, 8.0, 10.5);

 

In Class Activity – Functions Worksheet

 

  1. cout<<num1<<num3;

 

  1. cout<<cube(num1, num3);

 

  1. value = cube(num1, int2, num3);

 

  1. value = cube(7, 8, 9);

 

  1. Consider the following functions:

int secret(int x)

{

int i, j;

 

i = 2 * x;

if (i > 10)

j = x / 2;

else

j = x / 3;

 

return j-1;

}

 

int another(int a, int b)

{

int i , j;

 

j = 0;

for(i = a; i <= b; i++)

j = j + i;

 

return j;

}

 

What is the output of each of the following program segments?

  1. x = 10;

cout<<secret(x)<<endl;

 

  1. x = 5; y = 8;

cout<<another(x,y)<<endl;

 

  1. x = 10; k = secret(x);

cout<<x<<” “<<k<<” “<<another(x,k)<<endl;

 

  1. x = 5; y = 8;

cout<<another(y,x)<<endl;

 

In Class Activity – Functions Worksheet

 

  1. Consider the following function prototypes:

int test(int, char, double, int);

double two(double, double);

char three(int, int, char, double);

 

Answer the following questions.

 

  1. How many parameters does the function test have?

 

 

  1. What is the type of the function test?

 

 

  1. How many parameters does function two have?

 

 

  1. What is the type of function two?

 

 

  1. How many parameters does function three have?

 

 

  1. What is the type of function three ?

 

 

  1. How many actual parameters are needed to call the function test?

 

 

  1. What is the type of each parameter, and in what order should you use these parameters in a

call to the function test?

 

 

  1. Write a C++ statement that prints the value returned by the function test with the actual

parameters 5, 5, 7.3, and ‘z’.

 

 

  1. Write a C++ statement that prints the value returned by function two with the actual

parameters 17.5 and 18.3, respectively.

 

 

  1. Write a C++ statement that prints the next character returned by function three. (Use your

own actual parameters.)

 

 

 

 

In Class Activity – Functions Worksheet

 

  1. Consider the following function:

int mystery(int x, double y, char ch)

{

int u;

if(‘A’ <= ch && ch <= ‘R’)

return(2 * x + static_cast<int>(y));

else

return(static_cast<int>(2*y)-x);

}

 

What is the output of the following C++ statements?

 

  1. cout<<mystery(5,4.3,’B’)<<endl;

 

  1. cout<<mystery(4,9.7,’v’)<<endl;

 

  1. cout<<2*mystery(6,3.9,’D’)<<endl;

 

  1. Consider the following function:

 

int secret(int one)

{

int I;

int prod = 1;

 

for(I = 1; I <= 3; I++)

prod = prod * one;

return prod;

}

  1. What is the output of the following C++ statements?
    1. cout<<secret(5)<<endl;

 

  1. cout<<2 * secret(6)<<endl;

 

  1. What does the function secret do?

 

 

 

 

 

 

 

 

 

 

 

In Class Activity – Functions Worksheet

 

  1. Show the output of the following program.

#include <iostream>

int mystry(int);

int main()

{

int n;

 

for(n = 1; n <= 5; n++)

cout<<mystry(n)<<endl;

return 0;

}

 

int mystry(int k)

{

int x, y;

 

y = k;

for(x = 1; x <= (k – 1); x++)

y = y * (k – x);

return y;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(OVER)

  1. Show the output of the following program.

#include <iostream>

bool strange(int);

int main()

{

int num = 0;

 

while(num <= 29)

{

if(strange(num))

cout<<“True”<<endl;

else

cout<<“False”<<endl;

num = num + 4;

}

return 0;

}

bool strange(int n)

{

if(n % 2 == 0 && n % 3 == 0)

return true;

else

return false;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Due:    _________________________________

 

In Class Activity – One Dimensional Arrays Worksheet

 

  1. Declare a one-dimensional array that could be used to hold data for the salaries (float type) of 100 employees.

 

  1. Fill in the blanks below to print the entire scores array to the screen.

1     //print the scores2     for (int i = ____; i _______________; i++)3     {4           cout <<“The value of score[” << i << “] is “;5           cout << ___________________ << endl;6     }

  1. Write a program that reads a data file named scores.dat . The program should contain a function ReadScores that reads from the file scores.dat. The program should also contain an activation statement for a function named PrintScores. Add a function definition and a function prototype for PrintScores . The function should print the scores in the array scores.

 

 

 

 

 

 

 

 

 

 

 

  1. Write a function to determine the average of the array scores. Add an activation statement to determine the average of the array scores and add a cout statement to print the average.

 

 

 

 

 

 

 

 

 

In Class Activity – One Dimensional Arrays Worksheet

  1. Add a function to your program called AboveAverage(). This function should receive the array of scores, the number of scores, and the average score. The function should print all scores that are greater than the average.

 

 

 

 

 

 

 

 

 

  1. Based on the linear search in your notes, place the value of index after each of the following activation statements:

a.     index = LinearSearch (list, 12, 3);                       index =            ________________b.     index = LinearSearch (list, 12, 21);                     index =            ________________

 

  1. Add the LinearSearch() function to your program. In the main program, add a loop to enter values and search for them in the scores array using the function. In the loop, a value should be input. Then the function should be used to search for the value. Finally, the index where the value was found should be printed (or a message saying the value was not found). The loop should terminate when the sentinel data -99 is read.   Assume you are running the program using the data values: 55 10 59 70 -99

 

 

 

 

 

 

 

 

 

 

 

 

  1. Trace the calls to BinarySearch() in your notes and determine the index of each “guess” examined by this function. Also determine the value of index after the function returns.

For example, if you trace the call BinarySearch(list, 12, 10), the following is a possible answer:

Guess 1: middle = 5                        Guess 2: middle = 8                            Guess 3: middle = 6    Guess 4: middle = 7                        Function returns 7 a.      index = BinarySearch (list, 12, -2);                        b.      index = BinarySearch (list, 12, 100);

 

Due:    _________________________________

In Class Activity – Strings Worksheet

  1. C Strings declarations
  2. Show a declaration of a C string array variable large enough to contain your first (personal) name. Be sure to include room for the NULL terminating character. The array variable should be initialized to contain your first name.

 

  1. Show an internal representation of your first name.

    For example, to show the internal representation of “x=” on the answer sheet, one would type:

    x = \0

    0 1 2

 

  1. C String Comparisons
  2. For each of the following C string comparisons, tell whether strcmp() returns 0, a value less than 0 (a negative value), or a value greater than 0 (a positive value)?
  3. strcmp(“158”, “435”) ___________________
  4. strcmp(“abc”, “ABC”) ___________________
  5. strcmp(“Jim”, “Jimmy”) ___________________
  6. strcmp(“Brenda”, “Brenda”) ___________________

 

 

  1. Internal Representations
  2. Show the internal representation of str1 (in the same fashion as you did in the first question) and the

value of the variable length after each of the following statements, given the declarations:

char str1[5];                   int length; 1.      strcpy(str1, “Joe”);                          ___________________ 2.      length = strlen(str1);                       ___________________ 3.      strcpy(str1, “Joseph”);                    ___________________ 4.      length = strlen(str1);                       ___________________

In Class Activity – Strings Worksheet

 

  1. Suppose the C++ function strlen() didn’t already exist.
  2. Write code to implement your version of the strlen() function. That is, write a function to determine the length of a C string, str, supplied as an argument.
    •      Call your function MyStrLen().
    •     The function prototype for your function is:  int MyStrLen(char str[]);
  1. Write a main program to test your function. The main program should contain a sentinel-controlled loop that

(1)        Reads a C string,

(2)        Finds the length of the C string using your MyStrLen() function,

(3)        Prints the length of the C string.

  1. The loop should terminate when the sentinel string END is read.
  2. Remember to use the function strcmp() to test for the terminating condition.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Due:    _________________________________

 

_____

In Class Activity – Two-Dimensional Arrays Worksheet

  1. Show C++ statements to declare the following two-dimensional array:

Declare a two-dimensional array which can be used to store a yearly budget. Each row of the array corresponds to a particular budgeted item like rent, electric, etc. There are at most 15 items to be budgeted. Each column of the array corresponds to a month, January, February, etc. Of course there are 12 columns corresponding to the 12 months of the year. All the data to be placed in the array consists of real numbers.

 

 

 

 

 

 

 

 

  1. Suppose we wish to read in the data for our students’ scores but the file is organized differently. Instead

of all of one student’s labs appearing first, the file has all grades on lab 1 first, then all grades on lab 2, etc. How must the code above be changed to accommodate this new arrangement of data? Explain the difference.

 

 

 

 

 

 

 

 

 

  1. Look at the lecture notes about the labScores two dimensional array. Assume there is a program that contains declarations for the labScores array and contains a function which reads in data into this array from the file labscores.dat.  Write a function to print the scores so that each student’s labs appear on a separate line of output. Include a statement in your main program to call this function. Your output should be labeled as follows:

Student 1:  80  90  70  100  60  90  85  78  93  80  70  98  89  94   Student 2:  98  85 100   99  89  90  72   0  78  98 100  65   0  56   Student 3:  85  60  25….   .   .

In Class Activity – Two Dimensional Arrays Worksheet

4.         Add a function to your program called StudentAvg(), which finds and prints the lab average for each student in the class. A function prototype for this function is shown below. Activate this function from the main program.      void StudentAvg(int labScores [][MAX_LABS],             //IN:  Lab scores                   int numStudents,                                                //IN:  # of students in the class                   int numLabs)                                                       //IN:  # of labs recorded per student

 

 

 

 

 

 

 

 

 

 

  1. Add a function called labAvg(), which finds and prints the average score made on each individual lab.

Activate this function from the main program.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Due:    _________________________________

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
Open chat
1
You can contact our live agent via WhatsApp! Via + 1 929 473-0077

Feel free to ask questions, clarifications, or discounts available when placing an order.

Order your essay today and save 20% with the discount code GURUH