jubilantrich.com-cpp-logo

Free basic c++ tips and tricks that can help you understand the language.


Declarations in C++, we have variable declaration and function declaration.


let us also differentiate declaration from definition and initialization of variables in C++

 programming. They sound different but perform similar functions.

When we talk about declaration, what we should know is that it happens outside the main function i.e

main () and probably, on top of the it i.e in between the preprocessor and the main function.

Reasons for declaring variables is to eliminate multiple definitions of variables in functions or block

of codes and in different files during linking.

Functions can also be declared, and the example below shows how variables and functions are being declared in C++ programming;

Declarations of variable;

extern int a ,b, c, d ;
extern float x, y, z; etc

Declarations of function;

int jubilant();
int average();
int sum();
int mean();

Functions can also be called within the main function i.e main(), e.g
main()
{
 int  rich=jubilant();
}

Definition and initialization of Variables;

They both happen with main(){}
Examples;
int d=8, o=6, k= 4;
byte rich =100,000,000; etc

Putting everything into codes or a program form.......


  1. #include <iostream>
  2. using namespace std;
  3. // variable declaration

  4. extern int  a, b;
  5. extern long d, e;
  6. extern float f, p;
  7.  
  8. // function declaration
  9. int jubilant();
  10. float rich();

  11. int main()
  12. {
  13. //variable definition and initialization
  14. int a=1, b=2;
  15. long d=3,e=4;
  16. float f=5,p=6;

  17. // function call
  18. float dok=rich();
  19. int rich=jubilant();

  20. ...........STATEMENTS OR COMMANDS OR BLOCK CODES.......................

  21. }
  22. // function definition
  23. int jubilant()
  24. {
  25. .....block codes.....
  26. return 0;
  27. }
  28. float rich()
  29. {
  30. ...block codes.....
  31. return 0;
  32. }

Arithmetic Operators: 


jubilantrich.com-arithmetic-image
There are following arithmetic
 operators supported by C++
language

The use of arithmetic operators in c++ programming language must include care to avoid unclear statements.

let consider the following examples;
The %  modulus operator will divide the first given number by the second given number and return the remainder of the operators.
Let's take a look at the program below and execute it to see the out for ourselves.
#include<iostream>
When the above code is compiled and executed, it produces the following result: 



      a = b * c - d % e / f ;                    // this is unclear                    
       a = (b*c) - ((d % e) / f);              // this is clear                        

This is useful to determine if a number has an odd or even value
The ++ increment operator and -- operator alter the given value by and returns the resulting new value. These are most commonly used to count iterations in a loop. The ++ mainly increases a value by one either before it is used in the program line or after.
They can both be placed before(prefix) or after(postfix) the operand. if  ++ or -- is placed before the operand, it immediately changes the value before it is used in the program.
On the other hand, placing it after the operand, the value is used first and increment or decrement by     is added to the result.


 usingnamespace std;  
main() 
int a =21;
 int b =10;
 int c ;  
  c = a + b;  
 cout <<"Line 1 - Value of c is :"<< c << endl ;   
 c = a - b;   
 cout <<"Line 2 - Value of c is  :"<< c << endl ; 
   c = a * b;    
cout <<"Line 3 - Value of c is :"<< c << endl ;   
 c = a / b;   
 cout <<"Line 4 - Value of c is  :"<< c << endl ;   
 c = a % b;   
 cout <<"Line 5 - Value of c is  :"<< c << endl ; 
   c = a++;   
 cout <<"Line 6 - Value of c is :"<< c << endl ; 
   c = a--;   
 cout <<"Line 7 - Value of c is  :"<< c << endl ;
 return0;
 } 


Line1-Value of c is:31
 Line2-Value of c is:11
 Line3-Value of c is:210
 Line4-Value of c is:2
 Line5-Value of c is:1
 Line6-Value of c is:21
 Line7-Value of c is:22 


PROBLEM AND SOLUTION

jubilantrich.com-cplusplus-question






SOLUTION:

1  #include <iostream>
2  using namespace std;
3
4  double  average_x;
5  double  mean_y;
6  double m;
7  double b ;
8  int main()
9  {
10  int p;
11  double x, y,  sum_x, sum_y, sum_xx, sum_xy;
12  sum_x=0;
13  sum_y=0;
14  sum_xx=0;
15  sum_xy=0;
16
17
18  cout<< "Enter the number of paired values: ";
19
20  cin  >>p;
21
22  cout  <<p << " pairs are required:\n ";
23
24  for (int a = 1; a <= p; a++)
25  {
26    cout << "First pair " <<a<<" ? ";
27
28  cin  >> x  >>y;
29
30
31  sum_x = sum_x + x;
32  sum_y = sum_y + y;
33  sum_xx = sum_xx + x*x;
34  sum_xy=sum_xy + x*y;
35
36  }
37
38  average_x = sum_x/p;
39  mean_y = sum_y/p;
40  m = (sum_xy  - mean_y*sum_x)/(sum_xx -average_x*sum_x);
41  b =  mean_y  -  average_x*m;
42
43  cout  << "The least squares regression line is:\n";
44
45  cout<<"Y = "<< m <<" X + "<<"("<< b<<")" <<endl;
46  }