Jamin2112 attempts C++ solutions

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    C++
  • #31
Grep said:
Be careful of letting the requirements frame your approach to the implementation. In this case, a vector (*cough*stopusingarrays*cough*) is very much a good idea. But if that range was very large and would result in a sparse array of counts, then you might consider something else like a map.



First, no reason to pass in an array rather than a vector. If you're just missing being able to initialize a vector from a list of integers, then check out C++11 and use initializer lists. But don't let that affect the function signature, either way. I think for implementing that in C++, you should probably assume that 'array' means you should use a vector, and not forcing you into making a subpar choice. Perhaps the requirements would better be stated as '...integers in a list of integers'.

I'm also going to suggest you go back to advice I gave you about a month ago on your other code, because it still applies and I don't want to repeat myself. In this case, I suggest not leaving out the curly braces for the for loop body. As D H said recently, it invites Murphy's Law. And of course, put the for on its own line, and the body between those curly braces.

I'll just add that, when choosing different approaches, if it doesn't need to be super high performance and won't have a large impact on the overall program's performance, then I tend to prefer the simplest and most easily maintained solution (but not grossly inefficient and wasteful, of course!). The profiler can tell me where I need to optimize later during development.

Your sort seems wrong. Is this right: sort(arr, arr + n);? If you were using std::vector, you could use the std::sort algorithm: http://www.cplusplus.com/reference/algorithm/sort/ and set a comparator that will make it come out largest first. That makes it even more obvious, simpler and less error prone.

Thanks for the advice! I'm taking it to heart.

I'm not sure how to set a comparator. Can you show me?
 
on Phys.org
  • #32
Jamin2112 said:
Thanks for the advice! I'm taking it to heart.

I'm not sure how to set a comparator. Can you show me?

Sure, you'll note that URL I gave you to the STL sort from <algorithm> contains an example bit of code that should make how to do it fairly clear, I hope. It's a really useful site that I use all the time. A very small and hopefully obvious change to their example comparator function should be all you need.
 
  • #33
Grep said:
Sure, you'll note that URL I gave you to the STL sort from <algorithm> contains an example bit of code that should make how to do it fairly clear, I hope. It's a really useful site that I use all the time. A very small and hopefully obvious change to their example comparator function should be all you need.

Ah, I see. The solution should look something like this.

Code:
bool greater (int i, int j) { 
     return (i > j);
}

int sum_largest_n(std::vector<int> vec, int n) { 
    std::vector<int>::iterator itbeg = vec.begin();
    std::sort(itbeg, vec.end(), greater);
    int sum = 0;
    for (std::vector<int>::iterator it = itbeg ; it != itbeg  + 9; ++it)
      sum += *it;
    return sum;
}

But of course this doesn't use the fact that every number in vec is between 0 and 9.
 
  • #34
Jamin2112 said:
What sort of fencepost error did I make?
You made a fencepost error in your attempt to remove duplicated characters from a string. Think of the N characters as fenceposts, the N-1 consecutive pairs as rails in the fence.
 
  • #35
D H said:
You made a fencepost error in your attempt to remove duplicated characters from a string. Think of the N characters as fenceposts, the N-1 consecutive pairs as rails in the fence.

I think my mind is so consumed with algorithms (median of two sorted arrays, whether a linked list contains a cycle, sub array with maximum sum of elements, etc.) that I'm getting sloppy about the nitty-gritty stuff. I'm going to try to focus on simple problems in this thread and focus on doing them right.
 
  • #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.
 
  • #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

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K