store arrays using a pointer

elektro

Well-known member
  • Apr 18, 2011
    6,884
    9,413
    113
    Tropical Island Of Sri Lanka
    මට මේ ප්‍රශ්න ටිකට පොඩි සප් එකක් දෙන්න මචංලා..

    i. Write a C++ program to accept five integer values from the keyboard. The five values should be stored in an array using a pointer and display the elements of the array on the screen.

    ii. Modify the solution of problem i, in order to display the elements of the array in reverse order using a pointer.

    iii. Write a function that receives two numbers as an argument and display all prime numbers between these two numbers. Call this function from main( ) using pointers.

    මෙතනදි arrays එක්ක pointers භාවිත වෙනවා :) ගොඩක් programming කරන අයට මේකට ලැබෙන පිලිතුරු වැදගත් වේවි මම හිතන්නේ :rolleyes:
     

    Arkham Knight

    Member
    Mar 14, 2014
    1,627
    89
    0
    මම එකක් විසදුවා අනෙකට හොද Algorithm එකක් හදන්න ඕනේ බාගෙට කරන්න පුලුවන් නමුත් එතකොට ගතියක් නෑ.

    Code:
        cout << "Type five values from your keyboard" << endl;
        int kInput[5] = {0};
        for(int i=0;i<5;i++)
        {
            cin >> kInput[i];
            cin.ignore();
        }
    
        int *pInput = &sVar[0];
        for(int i=0;i<5;i++)
        {
            *(pInput + i) = kInput[i];
        }
    
        cout << "typed values" << endl;
        for(int i=0;i<5;i++)
        {
            cout << sVar[i] << endl;
        }
    
        cout << "press any key to exit" << endl;
        cin.get();
     

    Arkham Knight

    Member
    Mar 14, 2014
    1,627
    89
    0
    දෙවැනි එක මම Algorithm එකක් හදලා හොද විදියට හැදුවා එතකොට Array එක 1000+ ලොකු වුනත් මේක වැඩ එළට.

    Algorithm එකේ basic principle එක මෙතන පැහැදිලි කරලා ඇති.

    පළවෙනි space එකේ තියෙන එක යනවා temp variable එකකට
    ඊට පස්සේ last space එකේ තියෙන value එක යනවා පළවෙනි space එකට.
    ඊට පස්සේ මේක දිගටම ඒ විදියට වෙනවා.

    මේකේ පළවෙනි සහා last space එක අදුන ගන්නේ

    int temp = *(pInput + i);
    *(pInput + i) = *(pInput + numCount -1 - i);
    *(pInput + numCount -1 - i) = temp;

    මේ code එකෙන්

    point එකක් array එකකට point කරාම

    ඒකේ *pInput එක return කරන්නේ array එකේ පළවෙනි space එකේ value එකයි.
    දෙවැනි space එකට යන්න ඕනේනම්
    *(pInput + 1) දාන්න ඕනේ , 3 වැනි space එකට නම් *(pInput + 2)
    එතකොට හැම තිස්සේම count - 1= index number එකයි.
    මෙතන index number එක තමයි 1,2, කියලා යන්නේ.

    *(pInput + numCount -1 - i)
    එකෙන් කරන්නේ
    numCount = 5 (array එකේ values 5 තියෙනවා නම්)
    පළවෙනි iteration එකේදි i = 0

    එතකොට
    *(pInput + 5 -1 - 0) එක point out වෙන්නේ array එකේ
    *(pInput + 4) last space එකට 4 තමයි last space එකේ index එක (count - 1= index number )

    එතකොට මෙතන වෙන දේ තමයි පහළ තියෙන්නේ

    int temp = *(pInput + i); // 1
    *(pInput + i) = *(pInput + numCount -1 - i); // 2
    *(pInput + numCount -1 - i) = temp; // 3

    පින්තූරේ තියෙන 1,2,3 follow කරන්න එතකොට තේරුම් ගන්න ලේසියි


    idz85SE.png



    Code:
    void qTwo()
    {
        int *pInput = &sVar[0];
        int tempVar;
        int numCount = (sizeof(sVar)/sizeof(sVar[0]));
    
        //reverse the number order
        for(int i=0;i< numCount/ 2;i++)
        {
            int temp = *(pInput + i);
            *(pInput + i) = *(pInput + numCount -1 - i);
            *(pInput + numCount -1 - i) = temp;
        }
    
        cout << "typed values in reverse order" << endl;
        for(int i=0;i<5;i++)
        {
            cout << sVar[i] << endl;
        }
    }
     
    • Like
    Reactions: hancok

    DJvodka

    Well-known member
  • Mar 31, 2009
    3,375
    292
    83
    A land like no other
    Simple ne

    Code:
    ///////////////////////////////////////////
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int iArr[5];
    	int *ptr = iArr;
        
    	for(int i=0; i<5;i++){
    		cin >> iArr[i];
    	}
        
        for(int i=0; i<5; i++){
            cout << *ptr;
            ptr++;
        }
        
    	return 0;
    
    /////////////////////////////////////////
    
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int iArr[5];
    	int *ptr = iArr;
        
    	for(int i=0; i<5;i++){
    		cin >> iArr[i];
    	}
        ptr+=4;
        for(int i=0; i<5; i++){
            cout << *ptr;
            ptr--;
        }
        
    	return 0;
    }
    /////////////////////////////////////////
    #include <iostream>
    using namespace std;
    
    bool isPrime(int n) {
        bool ret = false;
        for(int j = 2; j < n; j++)
            if(n%j==0)
                return ret;
        return true;
      
    }
    //assuming x < y
    void display(int x, int y){
        int max = y>x?y:x;
        for(int i=y; i<=max;i++){
            if(isPrime(i))
        	cout << i << endl;
    	}
    }
    
    int main() {
        
        void (*fun)(int,int);
        fun=&display;
        fun(10,5);
        
    	return 0;
    }
    //////////////////////////////////////////
     

    elektro

    Well-known member
  • Apr 18, 2011
    6,884
    9,413
    113
    Tropical Island Of Sri Lanka
    ගොඩක්ම ගොඩක් පිං මචෝ... රෙප් දෙන්න බෑ පහල වගේ මෙසේජ් එකක් එනවා..මම දන්නවා උබ ඒවා බලාගෙන නෙමේ උදව් කළේ කියලා..ගොඩක් පිං..ජය වේවා !!! :)


    You must spread some Reputation around before giving it to Arkham Knight again.
     

    elektro

    Well-known member
  • Apr 18, 2011
    6,884
    9,413
    113
    Tropical Island Of Sri Lanka
    Simple ne

    Code:
    ///////////////////////////////////////////
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int iArr[5];
        int *ptr = iArr;
        
        for(int i=0; i<5;i++){
            cin >> iArr[i];
        }
        
        for(int i=0; i<5; i++){
            cout << *ptr;
            ptr++;
        }
        
        return 0;
    
    /////////////////////////////////////////
    
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int iArr[5];
        int *ptr = iArr;
        
        for(int i=0; i<5;i++){
            cin >> iArr[i];
        }
        ptr+=4;
        for(int i=0; i<5; i++){
            cout << *ptr;
            ptr--;
        }
        
        return 0;
    }
    /////////////////////////////////////////
    #include <iostream>
    using namespace std;
    
    bool isPrime(int n) {
        bool ret = false;
        for(int j = 2; j < n; j++)
            if(n%j==0)
                return ret;
        return true;
      
    }
    //assuming x < y
    void display(int x, int y){
        int max = y>x?y:x;
        for(int i=y; i<=max;i++){
            if(isPrime(i))
            cout << i << endl;
        }
    }
    
    int main() {
        
        void (*fun)(int,int);
        fun=&display;
        fun(10,5);
        
        return 0;
    }
    //////////////////////////////////////////

    thanx mcho :)