C/C++ Sequence Order Checker kodu olmuş mu?

SubhanXd

Centipat
Katılım
5 Kasım 2022
Mesajlar
186
Çözümler
1
Selamlar. Yapmam gereken :
"Given a sequence of integers, check if it is in ascending order, descending order, or neither. An ascending sequence is a sequence in which each element is greater than to the previous element. A descending sequence is a sequence in which each element is less than to the previous element. If none of these conditions are met, the sequence is neither ascending nor descending."

Yazdığım kod :
Kod:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;



int main() {
 int size;
 cin >>size ;
 int array [size] ;
 for (int f  = 0 ; f < size ; ++f) {
  cin >> array[f] ;
 }

  bool Ascending = false;
  for (int f = 1 ; f <size ; ++f) {
     if (array[f] > array[f-1]) {
      Ascending = true ;
 
     }
  }

  bool Descending = false ;
  for (int f  =1 ; f <= size- 1 ; ++f) {
    if (array[f] < array [f+1]) {
    Descending = true ;
 
    }
  }

   if  (Ascending  && Descending == false ) {
    cout << "Ascending" << endl ;
   }
   else if (Descending && Ascending == false) {
    cout << "Descending" << endl ;
   }
   else   {
    cout << "Neither" << endl ;
   }
   }
 
Direkt sormaktansa kendin yapmaya calisip kodunu atman guzel bir sey.
Direkt cevabi vermektense ipucu verecegim.
Bu problemi tek bir loop kullanarak cozebilirsin, hatta bazi durumlarda loop'un sonuna kadar iterate etmek zorunda da degilsin.
acs ya da desc durumunu false olarak initialize ediyorsun ve loop icinde true yapiyorsun. Fakat yeniden false a set eden bir mekanizma yok. Bunun uzerine dusunmen gerekiyor.
Ornek input denemen icin -> { 1, 2, 3, 2 ,2 1 }
Senin kodunda bu hem ascending hem de descending olarak isaretlenecek eger beynim beni yaniltmiyorsa :)

Ayrica sondaki boolean algebranin da duzenlemeye ihtiyaci var. Bunlarin sadece ve sadece biri true olmali loop bittiginde. ( mutually exclusive )
if asc -> return "asc"
else if desc -> return "desc"
else -> return "neither"
Seklinde yapamiyorsan loop'u duzgun kurgulamadin demektir.
 
C++:
 int size;
 cin >>size ;
 int array [size] ;

C++ da böyle dinamik dizi tanımlanmaz, stack overflow sebebidir. Ya new & delete kullan ya da vector kullan. Kodu gereksiz yere uzatmışsın, tek bir döngü içinde halledebilirsin.
 
Tek iterasyon yeterli gelir buraya. Ama bu tarz durumlarda olmuş mu diye sormak yerine test fonksiyonu yazmak senin için daha iyi olur.

Örneğin sekansların doğru ve yanlışlarına bakalım;

{ 1, 2, ,3 ,4 ,5 } artan sekans -> doğru
{ 5, 4, 3, 2, 1 } azalan sekans -> doğru
{ 5, 5, 3, 2, 1 } ikisi de değil -> doğru
{ 5, 4, 2, 3, 1} ikisi de değil -> doğru
Bunların testlerini yapacak bir fonksiyon yazalım (Not: Ben enumeration kullandım returnler için, array yerine de vector kullandım);
C++:
bool test(std::map<std::vector<int>, Sequence> test_cases) {
    for(auto test_case : test_cases) {
        if(isSequence(test_case.first) != test_case.second) {
            return false;
        }
    }
    return true;
}

int main() {
    std::map<std::vector<int>, Sequence> test_cases;
    test_cases[{1,2,3,4,5}] = Ascending;
    test_cases[{5,4,3,2,1}] = Descending;
    test_cases[{5,5,3,2,1}] = Neither;
    test_cases[{5,4,2,3,1}] = Neither;
    test_cases[{1,2}] = Ascending;
    test_cases[{3,2}] = Descending;
    std::cout << (test(test_cases) ? "PASSED" : "FAILED");
    return 0;
}
 
Son düzenleme:
Tek iterasyon yeterli gelir buraya. Ama bu tarz durumlarda olmuş mu diye sormak yerine test fonksiyonu yazmak senin için daha iyi olur.

Örneğin sekansların doğru ve yanlışlarına bakalım;

{ 1, 2, ,3 ,4 ,5 } artan sekans -> doğru
{ 5, 4, 3, 2, 1 } azalan sekans -> doğru
{ 5, 5, 3, 2, 1 } ikisi de değil -> doğru
{ 5, 4, 2, 3, 1} ikisi de değil -> doğru
Bunların testlerini yapacak bir fonksiyon yazalım (Not: Ben enumeration kullandım returnler için, array yerine de vector kullandım);
C++:
bool test(std::map<std::vector<int>, Sequence> test_cases) {
    for(auto test_case : test_cases) {
        if(isSequence(test_case.first) != test_case.second) {
            return false;
        }
    }
    return true;
}

int main() {
    std::map<std::vector<int>, Sequence> test_cases;
    test_cases[{1,2,3,4,5}] = Ascending;
    test_cases[{5,4,3,2,1}] = Descending;
    test_cases[{5,5,3,2,1}] = Neither;
    test_cases[{5,4,2,3,1}] = Neither;
    test_cases[{1,2}] = Ascending;
    test_cases[{3,2}] = Descending;
    std::cout << (test(test_cases) ? "PASSED" : "FAILED");
    return 0;
}
Daha bunları anlamadığım için bu şekilde yazmıyorum kodu kendim buldum ama yine de teşekkürler.
 

Technopat Haberler

Geri
Yukarı