My C++ Journey To become a Coder

Here I will be updating my daily c++ task which, I will do .

Variable is declared at the top but seen defined inside the main function.

//@ABHISHEK KUMAR
#include <iostream>
using namespace std;


extern int d, e;
extern int f;
extern float g;

int main(){
    //variable defination 
    int d, e;
    int f;
    float g;
    
    d = 20;
    e = 40;
    f = d + e;
    
    cout << f << endl;
    
    g = 90.0/4.0;
    cout << g << endl;
    
    return 0;
}


/* OUTPUT

60
22.5
*/

Some concept applied on function declaration we provide function at the time of deceleration and definition can be given anywhere.

// function declaration
int func();
int main() {
   // function call
   int i = func();
}

// function definition
int func() {
   return 0;
}

Conditional Statement

If 1 less than equal to n less than equal to 9, then print lowercase English word corresponding to number; otherwise print Greater than 9;

Code 1;

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int n;
    cin >> n;
    if(n==1)
        cout << "one";
    else if(n==2)
        cout << "two";
    else if (n==3)
        cout << "three";
    else if(n==4)
        cout << "four";
    else if(n==5)
        cout << "five";
    else if(n==6)
        cout << "six";
    else if(n==7)
        cout << "seven"; 
    else if(n==8)
        cout << "eight";
    else if(n==9)
        cout << "nine";
        else
        cout << "Greater than 9";
    return 0;     
    }               
            

Code 2

#include <iostream>
using namespace std;

int main() {
    string s[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    int n;
    cin >> n;
    cout << ((1 <= n and n <= 9) ? s[n - 1] : "Greater than 9");
    return 0;
}
Sample Input 

5

Expected Output

five

For Loop

Given Two positive integers a and b(a greater than b) separated by a new line.

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    string s[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
        if(1 <= i and i <= 9)
        cout << s[i-1] << endl;
        else
            cout << (i % 2 ? "odd" : "even") << endl;
    return 0;
}

Output

Sample Input 

8
11

Output

eight
nine
even
odd

Function in C++

The Syntax for the Function is

return_type function_name(arg_type_1 arg1, arg_type_2 arg2, arg_type3 arg3, arg_type_4 arg4,...){
...
...
...
   [if return type is non void return something]
   return something of type 'return_type';
}

Example Returning the greatest of the four integers.

#include <iostream>
#include <cstdio>
using namespace std;

int max_of_four(int a, int b, int c, int d){
    return max(max(a, b), max(c, d));
}

int main (){
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    return 0;
}    
    

OUTPUT

Sample Input

3
3
6
5

Sample Output

6

POINTER

Pointer which is used to share memory address among different primary functions.

#include <stdio.h>
#include <math.h>

void update(int *a,int *b) {
    // Complete this function   
    int t,x,y;
    x=(*a)+(*b);
    y=(*b)-(*a);
    if(y<0)
        y*=-1;
    *a=x;
    *b=y;
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}

Array in c++

Print in reverses order

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int n,i,*arr;
    cin >> n;
    arr= new int[n];
    for(i=0;i<n;i++)
        cin >> arr[i];
    for(i=n-1;i>=0;i--)
        cout<<arr[i]<<" ";    
    return 0;
}

Leave a Comment