user14443749
user14443749

Reputation: 35

How to count the number of times a loop occurs?

I'm creating a program that can find the sum from 1 to a number entered by a user using two methods. The first method is by physically adding every number between, and the second method is through the following formula: (x*(x+1))/2. I wrote both the functions out, but I can't figure out how to count the number of operations that occur for each method. For instance, there's a total of 3 mathematical operations that happen using the second formula. I've attempted to count the operations (shown below) but I'm not sure what exactly to use for this.

int sumEquation (int x, int operationCount) { 
    operationCount =3;
    return (x*(x+1))/2;
}
int sumOfDigits(int x, int operationCount) { 
    operationCount = 0;
    int sum = 0;
    for (int i = 1; i <= x; i++) {
        sum += i;
        operationCount +=2;
    } 
    return sum ; 
}
int main() {     
    int n;
    cout << "Enter a number: " ;
    cin >> n;
    int operationCount;    
    cout << "First Method" <<endl;    
    cout << "Result: " << sumOfDigits(n, operationCount) << " with "  << operationCount <<  " operations" << endl;             
    cout << "Second Method" <<endl;
    cout << "Result: " <<sumEquation(n, operationCount) << " with " << operationCount << " operations " <<endl;
    return 0;
}

Upvotes: 2

Views: 730

Answers (2)

hazy007
hazy007

Reputation: 21

Here either you can return a pair of ints in which one is the sum and one is the operation count. Or you can use an operation count variable as you have done. But then you will have to pass the variable through reference. Like-

int sumEquation (int x, int& operationCount) {
    operationCount =3;
    return (x*(x+1))/2;
}

int sumOfDigits(int x, int& operationCount) {
    operationCount = 0;
    int sum = 0;
    for (int i = 1; i <= x; i++) {
        sum += i;
        operationCount +=2;
    }
    return sum ;
}

Upvotes: 2

Vibhaas Srivastava
Vibhaas Srivastava

Reputation: 857

It's seems that you are already able to count the number of operations just fine but unable to return the result, so,

std::pair<int, int> sumEquation (int x) { 
    int operationCount =3;
    return {(x*(x+1))/2, operationCount};
}
std::pair<int, int> sumOfDigits(int x) { 
    int operationCount = 0;
    int sum = 0;
    for (int i = 1; i <= x; i++) {
        sum += i;
        operationCount +=2;
    } 
    return {sum, operationCount}; 
}
int main() {     
    int n;
    std::cout << "Enter a number: " ;
    std::cin >> n; 
    std::cout << "First Method" << std::endl;  
    auto method1 = sumOfDigits(n); 
    std::cout << "Result: " << method1.first << " with "  << method1.second <<  " operations" << std::endl;             
    std::cout << "Second Method" << std::endl;
    auto method2 = sumEquation(n); 
    std::cout << "Result: " << method2.first << " with " << method2.second << " operations " << std::endl;
    return 0;
}

Should do the trick. I used std::pair, but you could use other methods, like arrays, to achieve the same goal.

Note: Edited after this comment, here is the original code:

pair<int, int> sumEquation (int x) { 
    int operationCount =3;
    return make_pair((x*(x+1))/2, operationCount);
}
pair<int, int> sumOfDigits(int x) { 
    int operationCount = 0;
    int sum = 0;
    for (int i = 1; i <= x; i++) {
        sum += i;
        operationCount +=2;
    } 
    return make_pair(sum, operationCount); 
}
int main() {     
    int n;
    cout << "Enter a number: " ;
    cin >> n; 
    cout << "First Method" <<endl;  
    pair<int, int> method1 = sumOfDigits(n); 
    cout << "Result: " << method1.first << " with "  << method1.second <<  " operations" << endl;             
    cout << "Second Method" <<endl;
    pair<int, int> method2 = sumEquation(n); 
    cout << "Result: " << method2.first << " with " << method2.second << " operations " <<endl;
    return 0;
}

Upvotes: 2

Related Questions