CSE1146 Final Sorular ve Cevaplar

CSE1146 Final sınavının sorularını çözdüğüm gibi ekledim buraya. Yarın öbürgün güncellerim belki.

3 Sayı Değiştirici (15 puan)

Write a C function named void swap3Numbers(int *x, int *y, int *z) to swap three elements using call by reference. For example if the inputs are 5, 6, 7 then they should become 7, 5, 6. Do not attempt to solve this question without pointers!

void swap3Numbers(int *x,int *y,int *z) {
  /*Swap three numbers using pointers*/
  int temp = *x;
  *x = *z;
  *z = *y;
  *y = temp;
}
Seviye çok kolaydı.

Çeşitli Şeyleri Sayaç (25 puan)

Write a complete C++ program to count

  1. Number of words
  2. Number of spaces
  3. Number of vowels (both uppercase and lowercase vowels)
  4. Number of characters

that a string taken from the user.

Note that the letters a, e, i, o, and u are called vowels. The other letters in the alphabet are called consonants.

#include <iostream>
#include <string>
using namespace std;

int main() {
  string girdi;
  cout << "Enter the string:" << endl;
  getline(cin, girdi);
  cout << girdi << endl;

  char vowels[] = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
  int blank_count = 0;
  int vowel_count = 0;
  int length_count = 0;
  for (auto& karakter : girdi) {
    // Adding one to length
    length_count++;

    // Checking blanks
    // Note that blank_count + 1 gives word_count
    if (karakter == ' ') {
      blank_count++;
    }

    // Checking vovels
    for (auto& vowel : vowels) {
      if (vowel == karakter) vowel_count++;
    }

  }

  cout << "No. of words = " << blank_count + 1 << endl
       << "No. of spaces = " << blank_count << endl
       << "No. of vowels = " << vowel_count << endl
       << "No. of characters = " << length_count-blank_count << endl;
  return 0;
}
Zorluğu kolaydı.

Stack Sınıfı Yaratma (35 puan)

Write a complete C++ Program to implement your own stack. Your stack should be able to implement the following functions:

  1. push,
  2. pop,
  3. display

For each operation prompt the user as follows:

"Enter your choice:  "

You can define a simple menu to manage your stack; if user selects 1 in the menu, your program should make a push operation, for 2 pop operation and for 3 you should display the contents of your stack.

If user selects 1 to push something and the stack is full, then print the following message on the screen:

"Stack is full"

If user selects 2 to pull from the stack and the stack is empty, then print the following message on the screen:

"Stack is empty"

After each operation ask the user the following question:

"Do you want to continue? (y/n)", if user answers it with "y" for indicating yes, then continue with the execution, else if user answers it with "n" for indicating no, then stop the execution of your program immediately.

You must define at least one class in your program.

Hint-1: You may consider an integer array to store data, that is, each element in your stack is an integer.

Hint-2: You can assume the maximum size of your stack is 100, 1000 or something.

#include <iostream>
#include <string>
#define STACK_MAX_LENGTH 1000

using namespace std;

class Stack {
  string empty_msg = "Stack is empty";
  string full_msg = "Stack is full";
  int top;
  int stack_array[STACK_MAX_LENGTH];

  bool isEmpty() {
    return top == -1;
  }

  bool isFull() {
    return top == STACK_MAX_LENGTH-1;
  }

 public:
  // Constructor
  Stack() {
    for (int index = 0; index < STACK_MAX_LENGTH - 1; index++) stack_array[index] = 0;
    top = -1;
  }

  void push(int inserted) {
    if(!isFull()) {
      stack_array[++top] = inserted;
    }
    else cout << full_msg << endl;
  }

  void pop() {
    if(!isEmpty()) {
      cout << "Deleted value is " << stack_array[top] << endl;
      top--;
    }
    else cout << empty_msg << endl;
  }

  void display() {
    if(!isEmpty()) {
      // Traveling through array until the top.
      for (int pos = 0; pos <= top; pos++) {
        cout << stack_array[pos] << " ";
      }
      // Returning to new line since it is asked.
      cout << endl;
    }

    // If the stack is empty print it.
    // else cout << empty_msg << endl;
  }
};

int main()
{
  Stack _stack;

  cout << "1. push\n" << "2. pop\n" << "3. display\n";

  int menu_choice = -1;
  char continue_key = 'y';
  int int_to_do = 0;
  while (true) {
    // Checking quit status
    if (continue_key == 'n') break;

    // Getting user's desire
    cout << "Enter your choice:" << endl;
    cin >> menu_choice;
    cout << menu_choice << endl;

    // Menu operations
    if (menu_choice == 1) {
      cout << "Enter an integer element to push:" << endl;
      cin >> int_to_do;
      cout << int_to_do << endl;
      _stack.push(int_to_do);
    }
    else if (menu_choice == 2) {
      _stack.pop();
    }
    else if (menu_choice == 3) {
      _stack.display();
    }

    // Asking for new commands
    cout << "Do you want to continue? (y/n)" << endl;
    cin >> continue_key;
    cout << continue_key << endl;
  }

}
Zorluk kolaydı, hoca sonradan outputu değiştirdi. Ondan önce saçlarımı döktüm birazcık, ama hocadaymış hata :)

Test Soruları (5'er puan)

Bunlardan emin değilim, hocanın cümleyi kurma biçimden anlayamadım ancak çıkarım yapa yapa ilerleyerek şu cevapları verdim.

  • Which syntax of inheritance of class is given in the following? (Cevabımdan eminim.) – class name : access specifer class name
  • What is the unique property of the destructor? (Cevabımdan eminim.) – It has the name same as class
  • Derived class pointer would be treated as base class pointer. (Çoğunlukla eminim.) – False
  • In polymorphism, an object from base class can not be allocated by using new keyword. (Hiç emin değilim.) – False
  • Which of the following statement is true for virtual functions? (Çoğunlukla eminim.) – indicates, prototype of that member function is going to be used at following derived classes.