jeudi 31 mars 2016

Stanford C++ coursework. Gauss' childhood

Here I am doing Standford C++ coursework.

Exercise 03 from reader exercises. Here is the given question:

Exercise 03 Compute the sum of the numbers between 1 and 100.

/*
    As mathematical historians have told the story, the German mathematician
    Karl Friedrich Gauss (1777-1855) began to show his mathematical talent 
    at a very early age. When he was in elementary school, Gauss was asked by
    his teacher to compute the sum of the numbers between 1 and 100. Gauss is
    said to have given the answer instantly: 5050. Write a program that computes
    the answer to the question Gauss’s teacher posed.
 */   

The error I am getting when executing this program is this => Guassian.cpp: In function ‘int main()’: Guassian.cpp:29:12: error: invalid use of non-static member function ob1.sumodds;

What's wrong?

#include<iostream>
using namespace std;

class Guassian
{


public:

int sumodds ( int  last )
{
    int result =    0;
    int odd = 1;
    for ( int i =    0; i < last; i++ ) 
    {
        result +=   odd;
        odd += 2;
    }
    return            result ;

}

};

int main()
{
Guassian ob1;
ob1.sumodds;
//return 0;
}

Aucun commentaire:

Enregistrer un commentaire