Reputation: 75
Alright, so I'm getting an "error: expected primary-expression before the 'OTHER' token" on the line where my printf statement is. I've researched this issue and still not figured out what's going on. If you're wondering what's my program is supposed to be, by the way, I'm trying to calculate the average difference in time that it takes to find the most frequent element in an integer array, using two different methods. The first method is just an O(n^2) brute force method and the other puts the elements of the array into a binary tree whilst keeping a counter for the number of times each element is added.
#include <stdio.h>
#include <ctime>
#include <time.h>
#include "binArr.h" // Homemade binary tree class with the end of each branch containing a counter
// for how many times it has been reached.
// O(n^2) algorithm: Loop through array and count the number of each element
// by looping through again and checking every other element
// against it, updating, if necessary, a variable for the
// highest count and another for the corresponding element.
int mode (int* arr, int n) {
int most = arr[0];
unsigned mostCnt = 0;
for (unsigned i = 0; i < n; i++) {
int thisCnt = 0;
for (unsigned j = 0; j < n; j++) {
if (arr[j] == arr[i]) thisCnt++;
}
if (thisCnt > mostCnt) {
mostCnt = thisCnt;
most = arr[i];
}
}
return most;
}
void test_efficiency(const unsigned max_array_test_size, const unsigned tests_per_size) {
srand (time(NULL));
double avgTimeDiff = 0;
for (unsigned i = 1; i <= max_array_test_size; i++) {
int arr[i];
for (unsigned j = 0; j < i; j++) {
for (unsigned k = 0; k < tests_per_size; k++) {
for (unsigned m = 0; m < i; m++) arr[m] = rand() % j + 1;
}
clock_t start, stop;
double method1Time, method2Time;
start = clock();
int thisMode = mode(arr, sizeof(arr)/sizeof(int));
stop = clock();
method1Time = (stop - start) / CLOCKS_PER_SEC;
start = clock();
binArr B;
B.addArray(sizeof(arr)/sizeof(int), arr);
thisMode = B.getMost();
stop = clock();
method2Time = (stop - start) / CLOCKS_PER_SEC;
avgTimeDiff += method2Time - method1Time;
}
}
avgTimeDiff /= (max_array_test_size * max_array_test_size * tests_per_size);
printf("After %c tests, testing arrays up to a size of %c, \n
the average time difference between the brute force \n
method and binary tree method to find the mode of \n
an integer array is %f seconds",
tests_per_size, max_array_test_size, avgTimeDiff);
}
int main() {
const unsigned TESTS_PER_SIZE = 500; // Number of tests to be executed
const unsigned MAX_ARRAY_TEST_SIZE = 50; // Array size per test
test_efficiency(MAX_ARRAY_TEST_SIZE, TESTS_PER_SIZE);
/*
int arr[] = {9, 3, 2, 11, 87, 4, 3, 3, 3, 3, 3, 9, 21, 11, 91, 11, 9, 2, 9};
// Using the binary tree
binArr B;
B.addArray(sizeof(arr)/sizeof(int), arr);
std::cout << "The mode of arr, using the binary tree, is " << B.getMost() << std::endl;
// Using the basic O(n^2) algorithm
std::cout << "The mode of arr, using the binary tree, is " << mode(arr, sizeof(arr)/sizeof(int));
*/
return 0;
}
Upvotes: 0
Views: 427
Reputation: 20838
The main problem is that string literals don't span across multiple lines. You can quote each line like Maciej suggested. Another way to fix this issue is to use \
at the end of each line:
printf("After %c tests, testing arrays up to a size of %c, \n \
the average time difference between the brute force \n \
method and binary tree method to find the mode of \n \
an integer array is %f seconds",
tests_per_size, max_array_test_size, avgTimeDiff);
Note that this method preserves the whitespace in the string which could be handy depending on your use-case.
Upvotes: 0
Reputation: 1728
Try it like this:
printf("After %c tests, testing arrays up to a size of %c, \n"
"the average time difference between the brute force \n "
"method and binary tree method to find the mode of \n"
"an integer array is %f seconds",
tests_per_size, max_array_test_size, avgTimeDiff);
Upvotes: 1