Posts

Showing posts from March, 2022

2 Dimensional Character Array in C and C++

Image
 //@2D character array in C++ #include<iostream> using namespace std; int main() {     char wdays[7][10];     for(int i=0;i<7;i++)     {         cout<<"Enter weekday: "<<i+1;         cin>>wdays[i];     }     cout<<endl<<"Weekdays are :"<<endl;     for(int i=0;i<7;i++)         cout<<wdays[i]<<endl;     system("pause"); }//end of main Output:

2-Dimensional Array through pointer using C++

//Two Dimensional Array through Pointer in C++  #include<iostream> using namespace std; int main() {  int arr[2][3]={         {10,20,30},         {40,50,60}};         int* ptr;     ptr=(int*)arr;     cout<<sizeof(int);     cout<<endl<<"Values of the array:";     for(int i=0;i<6;i++)         cout<<*(ptr+i)<<" ";     cout<<endl<<"Now Enter 5 values to array:"<<endl;     for(int i=0;i<6;i++)     {         cin>>*(ptr+i);//because cin takes variable name to write not address     }     cout<<endl<<"Again Values of the array:";     for(int i=0;i<6;i++)         cout<<*(ptr+i)<<" ";     return 0; }//end of main

Use of 2-Dimensional Array in C through Pointer

Image
//2D array through pointer  #include<stdio.h> int main() {     int arr[2][3]={         {10,20,30},         {40,50,60}};//initializing 2D array int i; int *ptr;//declaring pointer     ptr=arr;//assigning base address     printf("\nValues of the array:\n");     for(i=0;i<6;i++)     {         printf("%d ",*(ptr+i));     }     printf("\n Enter 5 values to array:\n");     for(i=0;i<6;i++)     {         scanf("%d",(ptr+i));     }     printf("\n\nNow again values of array:\n");     for(i=0;i<6;i++)     {         printf("%d ",*(ptr+i));     }     printf("\n\n\n");         //cout<<**(arr+i)<<" "; return 0; }//end of main      Output:

Use of Character in C Language;

 Q:1 What is Character ? ans:     Character is an alphabet or symbol also like 'a','A','$','#' and '0','9'. Q: How C Language deals with characters. Ans: C Language deals with characters in the form of                               ASCII (American Standard code for Information Interchange) values. Q:What are the ASCII values of 'a','A','0' etc? Ans: ASCII value of 'a' =97, 'A' =65 and 0 = 48 and difference between them is of 32. Q: How we can store a character in C Program? Ans: char ch='a'; Program to use Character in C. #include<stdio.h> int main() {     char ch='a';     int i;     printf("\n value of ch is %c",ch);     printf("\n The ASCII value of a is %d",ch);     ch=ch-32;//putting ASCII value of A     printf("\n Now the value of ch is %c ",ch);     printf("\n ASCII value of A is %d",ch);     ch='0...

Program to demonstrate reference variable in C++

 //Program to demonstrate reference variable in C++ #include<iostream> using namespace std; int main() {     int num=10;     int& x=num;//creating reference of num     cout<<endl<<x;//value of x     cout<<endl<<num;//value of num     cout<<endl<<"address of num is "<<&num;     cout<<endl<<"Address of x is "<<&x;//same as the address of num }//end of main OUTPUT : 10 10 address of num is 0x61fe14 Address of x is 0x61fe14