0

If after addition, you have to divide by the third number, you need more than 3 variables. Why is that? can anyone please help me with this problem. it will be very grateful.why do we need to after addition, you have to divide by the third number, you need more than 3 variables. Why is that? thank you

#include <stdio.h> 
class res 
{ 
   int a[6],i; 
   public: 
   int result() 
   {
      for(i=0;i<3;i++) 
      { 
         if(a[i]%3==0) 
         {
            "sum=sum+a[i]";
         }
      }
   } 
};  // Added newly

int main() 
{
    res r; 
    int i,a[5];
    cout<<"enter three numbers"; 
    for(i=0;i<3;i++) 
    { 
       cin>>a[i]);
    } 
    r.result(); 
    return 0; 
}
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Post the code of what you are trying to do. – Mahesh Apr 06 '11 at 19:40
  • #include class res { int a[6],i; public: int result() {for(i=0;i<3;i++) { if(a[i]%3==0) {"sum=sum+a[i]"; } } int main() { res r; int i,a[5]; cout<<"enter three numbers"; for(i=0;i<3;i++) { cin>>a[i]); } r.result(); return 0; } – vinayak the great Apr 06 '11 at 19:41
  • i just want is the reason after addition of 3no you have to divide by the third number, you need more than 3 variables. Why is that? – vinayak the great Apr 06 '11 at 19:42
  • 1
    You can edit your original post to include that code, it will be so much more readable. – jonsca Apr 06 '11 at 19:43
  • Well, the first sentence is not true: you don't need more than 3 variables. In fact, you need no variables. Why not show us the statement that contains that error? Or show us the code you are trying? Thanks! – Pete Wilson Apr 06 '11 at 19:43
  • well addition of 3no and then the 3 no has to be divided with the sum of 3 no so why more then 3 variables are used – vinayak the great Apr 06 '11 at 19:45
  • What three variables are you talking about? What are you trying to do? – Starkey Apr 06 '11 at 19:54

1 Answers1

2

First you need to understand that the array variable in main is different from the class member a. And in result method, class variable a is not initialized with valid values to do a % operation on it.

if(a[i]%3==0) 
{
    "sum=sum+a[i]"; // And probably here you meant sum=sum+a[i];
                    // string should be enclosed in double quotes.
}

With the above modification made, class res doesn't know what the variable sum is.


Given the number of many other mistakes in your program, I suggest you to read a book from the suggested link.

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115