Jamin2112 attempts C++ solutions

  • C/C++
  • Thread starter Jamin2112
  • Start date
  • Tags
    C++
In summary: What if it's greater than the largest number? What if array[i] is negative? Just use array[-1], or update both the largest and second largest if necessary. What if it's greater than the largest number? Just use the largest number.
  • #36
Can someone give me an example code of how I should handle the case where an inputted array is empty? I'm wondering what the proper way is to do this. I promise it isn't for a homework assignment.
 
Technology news on Phys.org
  • #37
I'm keeping this thread alive since I need to keep my programming chops alive.

Taking an array of 0's and 1's and shoving all the 1's to the back:

Code:
#include <iostream>

void print_array(int* arr, int sz) { 
    std::cout << "(" << arr[0]; 
    for (int i = 1; i < sz; i++)  
       std::cout << "," << arr[i];  
    std::cout << ")\n"; 
} 

void zeros_to_front(int* arr, int sz) { 
    int sum(0), i(0);
    while (i < sz)
       sum += arr[i++];
    while (i > sz - sum - 1)
       arr[i--] = 1;
    while (i >= 0)
       arr[i--] = 0; 
} 

int main() {
   
   int myArray[] = {0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1};
   std::cout << "before:\n";
   int size = sizeof(myArray)/sizeof(int);
   print_array(myArray, size);
   zeros_to_front(myArray, size);
   std::cout << "after:\n";
   print_array(myArray, size);

   return 0;
}

Output:

Code:
before:
(0,1,1,0,1,0,0,0,0,1,1,0,1)
after:
(0,0,0,0,0,0,0,1,1,1,1,1,1)
 
  • #38
The mode of an array

Code:
#include <iostream>

void print_array(int* arr, int sz) { 
    std::cout << "(" << arr[0]; 
    for (int i = 1; i < sz; i++)  
       std::cout << "," << arr[i];  
    std::cout << ")\n"; 
} 

int mode(int* arr, int sz) { 
    std::map<int,int> cntMap;
    int most = arr[0]; 
    for (int i = 0; i < sz; i++) {
       if (!cntMap.count(arr[i])) {
          cntMap[arr[i]] = 0;
       } else {
          cntMap[arr[i]]++;
       }
       if (cntMap[arr[i]] > cntMap[most]) {
          most = arr[i];
       }
    }
    return most;
}int main() { 

   int myArray[] = {0, 1, 4, 4, 59, 3, 0, -1, -69, 69, 103, 103, 103, 2, 9, 1, 7, 5};
   std::cout << "the mode of\n";
   int size = sizeof(myArray)/sizeof(int); 
   print_array(myArray, size); 
   std::cout << "is " << mode(myArray, size);

   return 0;
}

Output:

Code:
the mode of
(0,1,4,4,59,3,0,-1,-69,69,103,103,103,2,9,1,7,5)
is 103
 

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top