C programming 1 Write a program that defines an array of int

C+ programming

1. Write a program that defines an array of integers and a pointer to an integer.

Make the pointer point to the beginning of the array.

Fill the array with values using the pointer (Note: Do NOT use the array name.).

Use pointer subscript notation to print out the array.

2. Write a program that initializes an array of characters with the phrase, “Take me to Clearwater Beach!”. Using pointers, scan the array to make each character upper case. The catch: you may NOT use the isupper(), islower(), toupper(), or tolower() functions. You must calculate whether a character is upper or lower case, and use the same logic to convert to upper case where applicable.

Hints:

*cptr >= ‘a’ && *cptr <= ‘z’

Assuming the ASCII value for ‘a’ is greater than ‘A’, how could you use an expression like, (‘a’ – ‘A’) to convert to upper case?

Solution

Programe 1 :

//CPP Programme to access array values using pointer
#include <iostream>
using namespace std;

int main () {                                     

   int abc[5];           // an array with 5 elements.
   int *p;           // integer pointer declaration

   p = abc;           //assigning array base address to pointer p

   // reading each array element\'s value using pointer
   cout << \"Array values using pointer \" << endl;
  
   for ( int i = 0; i < 5; i++ )       //loop to read all elements
   {      
   
      cin >>*(p + i) ;       //reading value to array elemnet using pointer          
   }

   // output each array element\'s value using pointer
   cout << \"Array values using pointer \" << endl;
  
   for ( int i = 0; i < 5; i++ )       //loop to display all elements
   {  
      cout << \"*(p + \" << i << \") : \";
      cout << *(p + i) << endl;       //display value of array elemnet using pointer  
   }



   return 0;
}

Sample Output for Programme1 :

Enter 5 Array values                                                                                                                                              

2                                                                                                                                                                        

45                                                                                                                                                                       

8                                                                                                                                                                        

6                                                                                                                                                                        

4                                                                                                                                                                        

Accessing Array values using pointer                                                                                                                                             

*(p + 0) : 2                                                                                                                                                             

*(p + 1) : 45                                                                                                                                                            

*(p + 2) : 8                                                                                                                                                             

*(p + 3) : 6                                                                                                                                                             

*(p + 4) : 4   

Programme 2 :

//CPP Programme to Covert String to Upper case using pointer
#include <iostream>       //inputoutput functions file
#include<string.h>       //string functions header
using namespace std;

int main ()           //main function begins execution
{                                     

   char abc[]=\"Take me to Clearwater Beach!\";           // an array with with given string.
   char *p;           // char pointer declaration

   p = abc;           //assigning array base address to pointer p



   // output each array element\'s value is converted to uppercase using pointer after
   cout << \"Accessing Array values using pointer \" << endl;
  
   for ( int i = 0; i<=strlen(abc); i++ )       //loop to convert & display all elements
   {  

        if(*(p + i)>=97 && *(p + i)<=122)       //check each cahr is lower case or upper case ,if it is lower then converts to upper
        {
           *(p + i)=*(p + i)-32;
              cout << *(p + i) ;  
        }
        else              
        cout << *(p + i) ;
         
   }



   return 0;
}

Sample Output :

Accessing Array values using pointer                                                                                                                                     

TAKE ME TO CLEARWATER BEACH!

C+ programming 1. Write a program that defines an array of integers and a pointer to an integer. Make the pointer point to the beginning of the array. Fill the
C+ programming 1. Write a program that defines an array of integers and a pointer to an integer. Make the pointer point to the beginning of the array. Fill the
C+ programming 1. Write a program that defines an array of integers and a pointer to an integer. Make the pointer point to the beginning of the array. Fill the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site