Webmail
Director web
::
Tutoriale
::
Code archive
::
Upload
::
Forum
::
Acasã
|
Ajutor
|
Cãutare
|
Autentificare
|
Înregistrare
» Utilizator
» Parteneri
» Linux Server
» WorldIT.info
» Web hosting
» Gazduire web
» IPFind
» Invatam.net
» PC Troubleshooting
» RegEx
» sigur.info
» Computericã
» De citit
» LAMP in CentOS
» LAMP in Ubuntu Server
» FAMP/LAMP in FreeBSD
» Formular de contact
» Optimizare Apache
» Optimizare MySQL
» Recomandari
» Hazardous Gaming
» What I Know
» PHP Funk
» Taskuri securitate
» Anti spam
» O parola eficienta
» Trojan, backdoor, spyware
» Phishing si pharming
» Securizare windows
» Cum functioneaza un virus?
» Certificate SSL
» Gumblar
» Un firewall eficient
» Ingineria sociala
» Statistici
64782
de mesaje.
7635
de topicuri.
1081
de utilizatori.
purcelllefevreo
e ultimul utilizator inregistrat.
[Detalii]
SkullBox Forum
|
Development
|
Java / C / C++ / C#
| Topic:
C/C++ Q&A
|
|
Pagini:
1
2
[
3
]
4
5
Print
C/C++ Q&A [4362 afisari]
astan
Mesaje: 766
Offline
Re: C/C++ Q&A
Code:
#include <cstring>
#include <iostream>
class BubbleSort
{
public:
/**
* @brief constructor
* @param arrayToSort the array to sort
* @param the number of elements in array
*/
BubbleSort(int arrayToSort[], int dimArray);
/**
* @brief destructor
*/
~BubbleSort();
/**
* @brief sort the array using the bubble sort method
*/
void Sort();
/**
* @brief display the content of the array
*/
void Display();
private:
/**
* the number of elements in m_array
*/
int m_dimArray;
/**
* the sorted array
*/
int* m_array;
};
BubbleSort::BubbleSort(int arrayToSort[], int dimArray)
{
m_dimArray = dimArray;
m_array = new int[m_dimArray];
std::memcpy(m_array, arrayToSort, m_dimArray * sizeof(int));
}
BubbleSort::~BubbleSort()
{
delete [] m_array;
}
void BubbleSort::Sort()
{
bool swapped;
int n = m_dimArray;
do
{
swapped = false;
--n;
for (int i = 0; i < n; i++)
{
if (m_array[i] > m_array[i + 1])
{
int temp = m_array[i];
m_array[i] = m_array[i + 1];
m_array[i + 1] = temp;
swapped = true;
}
}
}
while (swapped);
}
void BubbleSort::Display()
{
for (int i = 0; i < m_dimArray; i++)
std::cout << m_array[i] << std::endl;
}
int main()
{
int x [] = {32, 21, 66, 332, 85, 3, 4, 2, 10, 99, 45, 23};
BubbleSort bsorter(x, sizeof(x) / sizeof(int));
std::cout << "Before sorting ... " << std::endl;
bsorter.Display();
std::cout << "After sorting ... " << std::endl;
bsorter.Sort();
bsorter.Display();
return 0;
}
Output:
Code:
Before sorting ...
32
21
66
332
85
3
4
2
10
99
45
23
After sorting ...
2
3
4
10
21
23
32
45
66
85
99
332
Logged
21-08-2009, 23:26
Twitt
::
payne
Mesaje: 1244
Online
Re: C/C++ Q&A
vroiam doar constructorul nu si sortarea
Logged
WIK
-
What I Know
21-08-2009, 23:29
Twitt
::
astan
Mesaje: 766
Offline
Re: C/C++ Q&A
Hugh ? E un bubble sort amarat, nimic special.
Logged
21-08-2009, 23:32
Twitt
::
payne
Mesaje: 1244
Online
Re: C/C++ Q&A
Nu ,da vroiam sa il fac eu
Logged
WIK
-
What I Know
22-08-2009, 08:49
Twitt
::
payne
Mesaje: 1244
Online
Re: C/C++ Q&A
Cum fac o functie care sa returneze un array?
Ex in c#
Code:
int[] functie(int[] param)
{
return param;
}
Logged
WIK
-
What I Know
22-08-2009, 12:49
Twitt
::
AdyX
Mesaje: 1253
Offline
Re: C/C++ Q&A
Il returnezi ca parametru de iesire. Ar trebui sa pui la parametru un & in fata.
Logged
22-08-2009, 14:00
Twitt
::
payne
Mesaje: 1244
Online
Re: C/C++ Q&A
Mi-ai putea explica un pic mai pe larg
Logged
WIK
-
What I Know
22-08-2009, 15:41
Twitt
::
astan
Mesaje: 766
Offline
Re: C/C++ Q&A
Raspunsul simplu este:
- intorci un pointer
Code:
#include <iostream>
int* uselessFunc1(int arr[])
{
arr[0] = 9;
return arr;
}
void displayArray(int arr[], int dimArray)
{
for (int i = 0; i < dimArray; i++)
std::cout << arr[i] << std::endl;
std::cout << std::endl << std::endl;
}
int main()
{
int array[5] = {1,2,3,4,5};
displayArray(array, sizeof(array) / sizeof(int));
int *ptr = uselessFunc1(array);
displayArray(array, sizeof(array) / sizeof(int));
ptr[0] = 7;
displayArray(array, sizeof(array) / sizeof(int));
return 0;
}
Logged
23-08-2009, 01:13
Twitt
::
astan
Mesaje: 766
Offline
Re: C/C++ Q&A
Poti aborda o solutie generica, care sa functioneze pentru array-uri statice de orice tip si orice dimensiune. Si care sa imbunatateasca caracteristicile de type-safety fata de solutia anterioara, prin folosirea de referinte catre array-uri, in loc de solutia "normala" in care array-ul "decade" intr-un pointer prin pasarea in functia uselessFunc1. Totul folosind niste tehnici simple bazate pe template-uri.
Code:
#include <iostream>
template <typename T, int N>
struct wrapper
{
// typedefs to a reference to an array of a generic type, nested in a template class
typedef T (&refType)[N];
// assure const-correctness
typedef const T(&constRefType)[N];
};
template <typename T, int N>
typename wrapper<T, N>::refType uselessFunc2(typename wrapper<T, N>::refType arr, const T& value)
{
arr[0] = value;
return arr;
}
// display the content of a static array of a generic type
// the array size being a template parameter also
template <typename T, int N>
void displayArray(typename wrapper<T, N>::constRefType arr)
{
for (int i = 0; i < N; i++)
std::cout << arr[i] << std::endl;
std::cout << std::endl << std::endl;
}
int main()
{
int array[5] = {1,2,3,4,5};
// display the initial content of the array
displayArray<int, 5>(array);
// modify its first element at 9
wrapper<int, 5>::refType refArray = uselessFunc2<int, 5>(array, 9);
displayArray<int, 5>(array);
// modify the first element at 7
refArray[0] = 7;
displayArray<int, 5>(array);
// start over again
array[0] = 1;
// you can also do this, the end effect is to modify the first element at 101
uselessFunc2<int, 5>(array, 0)[0] = 101;
displayArray<int, 5>(array);
return 0;
}
Logged
23-08-2009, 01:45
Twitt
::
tercot
Mesaje: 1414
Offline
Re: C/C++ Q&A
Imi puteti explica care este treaba cu obiectele constante ale unui tip definit de utilizator ?
L.E.: Si despre
mutable
daca se poate.
Logged
"Fii totdeauna cu Dumnezeu, daca vrei ca Dumnezeu sa fie totdeauna cu tine!" Sfantul Ioan Gura de Aur
23-08-2009, 14:00
Twitt
::
LeOCruX
Mesaje: 322
Offline
Re: C/C++ Q&A
in ce tip de variabila pot sa retin rezultatul impartirii lui 2 la 4 (2/4);
am incercat double si float si imi da 0.00000 insa asta numai cu c (printf) cu streamuri nu merge, adica imi da 0.
p.s. disper
Logged
Un gram de practica face cat o tona de teorie
24-08-2009, 16:01
Twitt
::
payne
Mesaje: 1244
Online
Re: C/C++ Q&A
Eu inca nu am inteles ce incerci sa faci.
2/(4*(2/4)) ?
Logged
WIK
-
What I Know
24-08-2009, 16:08
Twitt
::
Razzor
Mesaje: 442
Offline
Re: C/C++ Q&A
Ia incearca :
Code:
double x=2.0/4.0;
2 si 4 sunt constante intregi, pe cand 2.0 si 4.0 sunt constante reale.
Logged
24-08-2009, 16:13
Twitt
::
LeOCruX
Mesaje: 322
Offline
Re: C/C++ Q&A
sa traiesti razzore
Logged
Un gram de practica face cat o tona de teorie
24-08-2009, 16:48
Twitt
::
tercot
Mesaje: 1414
Offline
Re: C/C++ Q&A
Ce este o functie inline, avantajele si dezavantajele folosirii acesteia.
Logged
"Fii totdeauna cu Dumnezeu, daca vrei ca Dumnezeu sa fie totdeauna cu tine!" Sfantul Ioan Gura de Aur
25-08-2009, 08:44
Twitt
::
Pagini:
1
2
[
3
]
4
5
Print
SkullBox Forum
|
Development
|
Java / C / C++ / C#
| Topic:
C/C++ Q&A
Powered by SMF 1.1.11
|
SMF © 2006-2009, Simple Machines LLC
Loading...