SlideShare a Scribd company logo
1 of 1
// convert string to int
int Misc::LocalAtoi(const string& str)
{
int val = 0;
// add values
for (auto const& c : str)
{
// 1 = 10, 10 = 100, etc
val *= 10;
// add curr variable (ie... 10 + 2 = 12, etc)
val += c - '0';
}
return val;
}
// sum 2 largest ints in an array
int Misc::Sum2LargestInts(int* arr, int _count)
{
// manual sorting
#if 1
// loop through the array
for (int i = 0; i < _count; i++)
{
int idx = 0;
// search for larger value
for (int j = 0; j < _count; j++)
{
if (arr[j] > arr[i])
idx = j;
}
// swap values if needed
if (idx > 0)
{
int swap = arr[i];
arr[i] = arr[idx];
arr[idx] = swap;
}
}
// return sum of 2 largest values
return arr[0] + arr[1];
#endif
// using std::sort
#if 0
// copy _arr into a local vector
vector<int> vArr(arr, arr + _count);
// sort (descending)
sort(vArr.begin(), vArr.end(), greater<int>());
// return sum of 2 largest values
return vArr[0] + vArr[1];
#endif
}

More Related Content

What's hot (16)

Var
VarVar
Var
 
Practica 4 errores
Practica 4 erroresPractica 4 errores
Practica 4 errores
 
Vatesh
VateshVatesh
Vatesh
 
Tmug Slide
Tmug SlideTmug Slide
Tmug Slide
 
Kruskal algorithm
Kruskal algorithmKruskal algorithm
Kruskal algorithm
 
Wave ECG
Wave ECGWave ECG
Wave ECG
 
Practica 10
Practica 10Practica 10
Practica 10
 
Co w
Co wCo w
Co w
 
3.3
3.33.3
3.3
 
Yohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisisYohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisis
 
Eliminación gaussiana java codigo
Eliminación gaussiana java codigo Eliminación gaussiana java codigo
Eliminación gaussiana java codigo
 
Kelompok 2.6
Kelompok 2.6Kelompok 2.6
Kelompok 2.6
 
elastic waist tip
elastic waist tipelastic waist tip
elastic waist tip
 
Kelompok 2.6
Kelompok 2.6Kelompok 2.6
Kelompok 2.6
 
Trabajo
TrabajoTrabajo
Trabajo
 
Vcs12
Vcs12Vcs12
Vcs12
 

More from George Scott IV

More from George Scott IV (7)

Square selection function
Square selection functionSquare selection function
Square selection function
 
Save game function
Save game functionSave game function
Save game function
 
Delete save from folder function
Delete save from folder functionDelete save from folder function
Delete save from folder function
 
Trees
TreesTrees
Trees
 
Strings
StringsStrings
Strings
 
Linked lists
Linked listsLinked lists
Linked lists
 
Arrays
ArraysArrays
Arrays
 

Misc

  • 1. // convert string to int int Misc::LocalAtoi(const string& str) { int val = 0; // add values for (auto const& c : str) { // 1 = 10, 10 = 100, etc val *= 10; // add curr variable (ie... 10 + 2 = 12, etc) val += c - '0'; } return val; } // sum 2 largest ints in an array int Misc::Sum2LargestInts(int* arr, int _count) { // manual sorting #if 1 // loop through the array for (int i = 0; i < _count; i++) { int idx = 0; // search for larger value for (int j = 0; j < _count; j++) { if (arr[j] > arr[i]) idx = j; } // swap values if needed if (idx > 0) { int swap = arr[i]; arr[i] = arr[idx]; arr[idx] = swap; } } // return sum of 2 largest values return arr[0] + arr[1]; #endif // using std::sort #if 0 // copy _arr into a local vector vector<int> vArr(arr, arr + _count); // sort (descending) sort(vArr.begin(), vArr.end(), greater<int>()); // return sum of 2 largest values return vArr[0] + vArr[1]; #endif }