CS35 Quiz 1 practice

Intro to C++ syntax
Trace through the following code, show the final output and show the contents of all stack frames prior to the return of maxSquares
#include <cstdio>

using namespace std;

int maxSquares(int x, int y, int* zp){
  if (x>y){
    return 0;
  return 1;

int main(){
  int a = 4;
  int b = -5;
  int c, ans;
  printf("a=%4d b=%4d\n" , a ,b);
  ans = maxSquares(a,b,&c);
  printf("a=%4d b=%4d\n" , a ,b);
  printf("c=%4d ans=%4d\n" , c ,ans);
  return 0;

Write a function that checks if an array of integers is sorted in decreasing order. You function should accept the array as a parameter, plus any other necessary parameters you want to include. Your function should return a Boolean value that is true if the array is sorted.

Write a main function that calls your function and displays the result

Write a Counter class that keeps track of a single integer counter. Your class should have an increment method that increases the counter value by one and a getCount method that returns the current count. New counter objects should have there count initialized to 0. Show what code would go in a .h file and what code would go in a .cpp file

Write a main function that creates two counter objects and tests all functionalilty of the class

This question is a bit challenging but will really test your understanding of pointers. Below is a snippet of code

int a=10,b=5,c=1;
printf("a=%d, b=%d, c=%d\n",a,b,c);
//prints a=1, b=5, c=10
Write the function sort_three such that the printf will always print a,b, and c in sorted order regardless of their values. You may use the swap_ptr function we discussed in class as part of your solution without writing that code.