arrays and pointers arithematic in c++

 




#include<iostream>
using namespace std;

int main() {
    /*###AN ARRAY IS A COLLECTION OF ITEMS OF SIMILAR TYPES STORES IN CONTIGUOS(CHIPAK CHIPAK KE) MEMORY LOCATION
###SOMETIMES A SIMPLE VARIABLE IS NOT ENOUGH TO HOLD ALL THE DATA
###FOR EXAMPLE hume 2500 students ke marks store krne h so 2500 variables bnana fesile nhi hoga
###to solve this problem we can define an array with size 2500 that can hold the marks of all students
###ginti 0 se start hogi ye c++ ka rule hai, 0,1,2... indices hote h*/

    /********ARRAYS EXAMPLE********/

    //int marks[4] = {5, 7, 57, 75}; //bracket m 4 likhna optional hai kyuki compiler khud hi figure out krelta hai
    //cout<<marks[0]<<endl;
    //cout<<marks[1]<<endl;
    //cout<<marks[2]<<endl;
    //cout<<marks[3]<<endl;

//yaha humne marks naam ka ek array bnaya jo continuous data store kr raha h
//another way to makr arrays

    int MathMarks[4];
    MathMarks[0]=27;
    MathMarks[1]=77;
    MathMarks[2]=28;
    MathMarks[3]=29;
    cout<<"these are math marks: "<<endl;
    cout<<MathMarks[0]<<endl;
    cout<<MathMarks[1]<<endl;
    cout<<MathMarks[2]<<endl;
    cout<<MathMarks[3]<<endl;

//we can also update the value of arrays

    int marks[4] = {5, 7, 57, 75};
    // marks[2]=777;
    // cout<<marks[0]<<endl;
    // cout<<marks[1]<<endl;
    // cout<<marks[2]<<endl;
    // cout<<marks[3]<<endl;

    /*****PRINTING ARRAYS USING LOOPS******/

    // for (int i=0 ; i<=3 ; i++)
    // {
    //     cout<<"the value of marks of "<<i<<" is "<<marks[i]<<endl;
    // }

    //solution to the quick quiz: doing the same thing using while and do-while loop

    //while loop:
    int i=0;
    // while (i<=3)
    // {
    //     cout<<"the value of marks of "<<i<<" is "<<marks[i]<<endl;
    //     i++;
    // }
    //do while loop:

    // do{
    //     cout<<"the value of marks of "<<i<<" is "<<marks[i]<<endl;
    //     i++;
    // } while(i<=3);

    /************POINTERS AND ARRAYS and pointers arithematic IN C++*************/

/*arrays ka address nikalne ke liye & krna wrong hai
arrays mein naam hi hmara address hota h. marks-->address of first block*/

    int* p=marks; //p ek pointer hai jo pehla element hmara marks ka h uspe, mtlb uspe point kr rha h
    cout<<"the value of marks[0] "<<*p<<endl; //dereferencing operator
    cout<<"the value of marks[1] "<<*(p+1)<<endl;
    cout<<"the value of marks[2] "<<*(p+2)<<endl;
    cout<<"the value of marks[3] "<<*(p+3)<<endl;
   
    cout<<*(p++)<<endl;
    //yaha p pe jo value h vo print hojaegi then p incriment hojaega
    cout<<*p<<endl; //ye incrimented pe hai
    cout<<*(++p)<<endl;

    return 0;
} *******FOR HOW COMPUTER STORES DATA AND BITS AND BYTES UNDERSTANDING WATCH******* https://www.youtube.com/watch?v=p3q5zWCw8J4 https://www.youtube.com/watch?v=01jkT5nSgRc

Comments

Popular posts from this blog

Function Overloading in C++

oops recall, basic operations and nesting of member functions

OBJECT ORIENTED PROGRAMMING, classes public and private access modifiers (syntax)