October 2018 1 24 Report

Czy te dwa kody w C++ są poprawne? Jeśli tak, to gdzie i jak można to poprawić?

Sortowanie przez wstawianie:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
using namespace std;

void zamien (int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
void wyswietl (int tab[], int n)
{
for (int i=0; i<n; i++)
{
cout<<setw(3)<<tab[i];
cout<<endl;
}
}

void sortowanie_przez_wstawianie (int tab[], int n)
{
int i, k, elem;
for (i=1; i<n; i++)
{
elem=tab[i];
k=i-1;
while (k>0 && tab[k]>elem)
{
tab[k=1]=tab[k];
k--;
}
tab[k+1]=elem;
}
}
int main ()
{
int tablica[5];
int i;
for (i=0; i<5; i++)
cout<<"Podaj wartosc: "<<endl;
wyswietl (tablica,5);
cout<<"Posortowane: "<<endl;
sortowanie_przez_wstawianie(tablica,5);
wyswietl (tablica,5);
cin.ignore ();
getchar ();
return 0;
}

Sortowanie przez wybór:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
using namespace std;

void zamien (int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}

void sortowanie_przez_wybor(int tab[], int n)
{
int i, j, k, temp;
for (int i=0; i<n; j++)
{
k=i;
for (j=i+1; j<n; j++)
if (tab[j]<tab[k])
k=j;
zamien(tab[k],tab[i]);
}
return;
}

void wyswietl (int tab[], int n)
{
for (int i=0; i<n; i++)
{
cout<<setw(3)<<tab[i];
cout<<endl;
}
}

int main ()
{
int tablica[5];
int i;
for (i=0; i<5; i++)
cout<<"Podaj wartosc: "<<endl;
wyswietl (tablica,5);
cout<<"Posortowane: "<<endl;
sortowanie_przez_wybor(tablica,5);
wyswietl (tablica,5);
cin.ignore ();
getchar ();
return 0;
}


More Questions From This User See All

Recommend Questions



Life Enjoy

" Life is not a problem to be solved but a reality to be experienced! "

Get in touch

Social

© Copyright 2013 - 2024 KUDO.TIPS - All rights reserved.