Question 1:
The C++ program below is supposed to read in a block of text from the user and check for pairs of identical lines.

Your task is to identify all the syntax and logic errors in the program.

#include <cstdio>

const int Len = 80;
typedef char string[80];

bool compare(string str1, string *str2);
{
   int i = 0;
   char c;
   while (c != '\0') {
      if (str1[i] != str2[i]) return true;
      i++;
      c = str2[i];
   }
   return false;
}

void fillparagraph(string text[])
{
   printf("Please enter %d lines of text\n", LINES);
   for (int i = 0; i < LINES; i++) {
       scanf("%s", text[i]); 
   }
}

int main()
{
   const int LINES = 24;
   const int LINESIZE = 80;
   string paragraph[LINES];
   void fillparagraph(*paragraph);
   for (i = 0; i < LINES; i++) {
       if (compare(paragraph[i], paragraph[i+1] == 0) 
          printf("lines %d and %d", i, (i+1));
          printf(" are the same\n");
   }
}


Question 2:
Write a function with the following behaviour:

The function takes an integer size as a parameter and, if the size is greater than 0, dynamically allocates an array of floats of the specified size.

If the allocation is successful then the function must initialize each array element with a value of 0.

Finally, a pointer to the allocated space is returned.


Question 3:

Rewrite the C++ program below to follow appropriate design principles (appropriate use of functions, constants, error-checking, etc).

#include <cstdio>

struct Table {
   int dataMatrix[5][10];
};

int main()
{
   Table t;
   int r, c, i;
   for (r = 0; r < 5; r++) {
       for (c = 0; c < 10; c++) {
           printf("Enter an integer\n");
           scanf("%d", &t.dataMatrix[r][c]);
       }
   }
   for (r = 0; r < 5; r++) {
       for (c = 0; c < 10; c++) {
           printf("%d,", t.dataMatrix[r][c]);
       }
       printf("\n");
   }
   return 0;
}


Question 4:
(i) Define a struct to hold the following fields (as part of a staff parking registration system):

(ii) Implement a function that dynamically allocates one of your structs, fills the values with user input, and returns a pointer to the newly created item.


Question 5:
The C++ program shown below is intended to create, fill, and display a linked list of numeric values.

Your task is to provide the prototypes and implementations for FillList and DisplayList functions.

#include <cstdio>

struct listElement {
   float data;
   listElement *next;
};

int main()
{
   listElement *front = NULL;
   listElement *back = NULL;
   printf("Please enter your sequence of positive numbers,\n");
   printf("followed by a 0 to end the sequence.\n");
   FillList(front, back);
   printf("\nThe sequence you entered was:\n");
   DisplayList(front, back);
   return 0;
}


Question 6:
Show the complete output from the following C++ program:

#include <cstdio>

void swap(int &x, int &y)
{
   int temp = x;
   x = y;
   y = temp;
}

float f1(int &x, int y) 
{  
    swap(x, y);
    x = x + y;
    return (y / x);  
}

float f2(float &x, float &y)
{
    y = x + y;
    return (x / y);  
}

int main()
{
   int a = 1;
   int b = 2;
   int c = f1(a, b);
   float d = 1;
   float e = 2;
   float f = f2(d, e);
   printf("%d, %d, %d, %g, %g\n", a, b, c, e, f);
   return 0;
}