CSE1146 Vize II Sorular ve Cevaplar

8 June 2021

Soru 1: Belirli Bir Aralıkta Gizemli ve Asal Sayı Bulucu

Write a complete C++ program that determines the prime and mysterious numbers between two numbers in ascending order given by the user.

If the given two boundary numbers are not provided in ascending order, terminate the program with an error message of :

The numbers you entered are not in ascending order.

Hint 1: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Hint 2: A mysterious number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is a mysterious number. Because 1^3 +3^3+5^3 = 153.

Yaklaşık Zorluk: Orta

Cevap 1:

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

int is_prime(int sayi) {
  if (sayi == 1) {
    return 0;
  } else if (sayi == 2) {
    return 1;
  } else {
    for (int i = 2; i < sayi; i++) {
      if (sayi % i == 0) return 0;
    }
  }
  return 1;
}

int is_mysterious(int sayi) {
  string str_sayi = to_string(sayi);
  int sum = 0;
  for (auto& iter : str_sayi) {
    int num = iter - '0';
    sum += num*num*num;
  }

  if (sum == sayi) {
    return 1;
  } else {
    return 0;
  }
}

int main() {
  int alt, ust;
  cout << "Enter two numbers in ascending order:" << endl;
  cin >> alt >> ust;
  cout << alt << " " << ust << endl;

  // Terminating the program.
  if (alt > ust) {
    cout << "The numbers you entered are not in ascending order." << endl;
    exit(0);
  }

  // Finding primes
  cout << "Prime Numbers between " << alt << " and " << ust << endl;
  for (int i = alt+1; i < ust; i++) {
    if (is_prime(i)) {
      cout << i << endl;
    }
  }

  // Finding mysterious
  cout << "Mysterious Numbers between " << alt << " and " << ust << endl;
  for (int i = alt+1; i < ust; i++) {
    if (is_mysterious(i)) {
      cout << i << endl;
    }
  }
  return 0;
}

Soru 2: "a"ları Yazmayı Sevmeyen Fonksiyon

Write a C function named void skip_a(FILE *fp,char *in) which prevents to write lower case letter 'a' to a file within any input. Assume that file opening and closing operations are already handled outside the function.

Yaklaşık Zorluk: Çok Kolay (daha güzel bir mantıkla yazanlar için Orta)

Cevap 2:

void skip_a(FILE *fp,char *in){
  char yazilacak[20];

  int yazilacak_points = 0;
  for (int i = 0; i < 20; i++) {
    if (in[i] != 'a') {
      yazilacak[yazilacak_points] = in[i];
      yazilacak_points++;
    }
  }

  fputs(yazilacak, fp);
}

Bundan daha iyi bir program yazabilirdim, örneğin bu programın en büyük hatası yazilacak karakter dizisinin sınırı olması. Ben bunu 20 ile denedim, işe yaradı. Yaramasaydı 1000 yapardım belki. Ancak eğer ki gerçek hayata uygun bir program yazmamız gerekirse bu yaklaşımı kullanmak çok büyük hata olur.

Soru 3: Sezar Şifreleme Yazılımı

Write a complete C++ program to encrypt a string.
You are asked to encrypt a given string by the user with the following requirements.

  • if a character in string is lower case letter, make it upper case and then get the second next character, for example a -> C.
  • if a character in string is upper case letter, make it lower case and then get the second next character, for example B -> d.
  • if a character in string is not a letter, change it with *.

Hint: You can check whether a character is lower-case or not with int islower ( int c ) built-in function. Moreover, you can convert a lower-case character to upper-case character by using int toupper ( int ch ) built-in function.

Yaklaşık Zorluk: Kolay

Cevap 3:

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

int main() {
  string girdi;
  cin >> girdi;
  cout << girdi << endl;

  for (auto& iter : girdi) {
    if (isalpha(iter)) {
      if (islower(iter)) {
        iter = toupper(iter) + 2;
      } else {
        iter = tolower(iter) + 2;
      }
    } else {
      iter = '*';
    }
  }
  cout << girdi << endl;
}

Soru 4: Kutu Sınıfı Yaratma ve Bunu Kullanma

Write a complete C++ program that creates the class Box. You are asked to calculate the volume and area of a box that its length, breadth and height values are taken from the user as an integer number.

Yaklaşık Zorluk: Çok Kolay

Cevap 4:

#include <iostream>
using namespace std;

class Box {
 public:
  int length;
  int breadth;
  int height;
};

int main() {
  Box deneme;        // Declare your object of type Box
  int a, b, c;
  cin >> a >> b >> c;
  deneme.length = a;
  deneme.breadth = b;
  deneme.height = c;
  cout << "Box1: " << a << " " << b << " " << c << endl;
  cout << "Volume of Box1: " << deneme.height*deneme.breadth*deneme.length << endl;
  cout << "Area of face 1: " << deneme.length*deneme.breadth << endl;
  cout << "Area of face 2: " << deneme.length*deneme.height << endl;
  cout << "Area of face 3: " << deneme.height*deneme.breadth << endl;
}

Soru 5: Bir Yazıdaki Karakter Sıklığını Bulan Program

Write a C++ program which reads a string on the first line and a char on the second line from the user and then prints the frequency of this char in the given string.

Hint:  To do this, size() function can be used to find the length of a string object.

Yaklaşık Zorluk: Çok Kolay

Cevap 5:

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

int main() {
    string yazi;
    char aranacak;

    getline(cin, yazi);
    cin >> aranacak;

    int count = 0;
    for (auto& iterator : yazi) {
      if (iterator == aranacak) count++;
    }

    cout << count << endl;
}

Published in cppders notları