Pages

Showing posts with label DMA. Show all posts
Showing posts with label DMA. Show all posts

Saturday, June 29, 2013

Double pointer 2d array c++

/* QUESTION:
Double pointers use write a program to store alphabets to a table (length of table during program execution) using

double pointer. Pass the table as arguments to a function. Print the vowels to the screen.

Hint: Your will use new operator to allocate memory.

Note: You have to release the memory using delete operator before the end of program.
*/

 // CODE :

 #include<iostream>
using namespace std;

void find_vowels(char **ptr,int a,int b);
int main(){
int a,b;
cout<<"Enter the no of rows your table for storing alphabets ";
cin>>a;
 char **ptr = new char *[a];

 cout<<"Enter the no of coloumns";
cin>>b;
 for (int i=0;i<a;i++){
  ptr[i] = new char[b];
 }
 cout<<"\n\nEnter the characters\n";
 for (int j=0;j<a;j++){
  for(int k=0;k<b;k++){
      cin>>ptr[j][k];
  }
 }
 cout<<"\n The vowels out of table are\n\n";
 find_vowels(ptr,a,b);
for(int p=0; p< a;p++)
delete [] ptr[p];
delete [] ptr;
 return 0;
}
void find_vowels(char **ptr,int a,int b){
 for (int l=0;l<a;l++){
  for(int m=0;m<b;m++){

   if (ptr[l][m] == 'a'||ptr[l][m] =='e'||ptr[l][m] =='i'||ptr[l][m] =='o'|| ptr[l][m] =='u'){
    cout<<ptr[l][m]<<endl;

   }


  }


 }
}

Friday, June 28, 2013

C++ new operator example

//QUESTION :

/*
Write a program to allocate memory locations for four integer values using ‘new‘operator.

Stores the addresses of allocated memory locations into array of pointers. Find out the

minimum values from four values.
Hint: You will use integer pointer to create integers and array of pointers to store addresses of

integers. You are required to use array of pointers to select minimum value integer.
 
Note: You have to release the memory using delete operator before the end of program.

*/


// CODE :

#include <iostream>
using namespace std;
int main(){

 int *ptr = new int [4];
 cout<<"Enter the values in array\n\n\n\n";
 for (int j = 0; j<4; j++){
  cin >> ptr[j];
 }
 int **arrayOfPtr = new int* [4];
 for (int i = 0; i<4; i++){
  arrayOfPtr[i] = (ptr+i);
 }
 int min = **arrayOfPtr;
 for (int k = 0; k<4; k++){
  if ( min>**(arrayOfPtr+k) ){
   min = **(arrayOfPtr+k);
  }
 }
 cout << "Minimum: "<< min << endl;
 for (i=0; i<4; i++)
  delete arrayOfPtr[i];
 delete [] arrayOfPtr;
 arrayOfPtr = NULL;
 return 0;
}

Thursday, June 27, 2013

C++ CODE FOR DYNAMIC MEMORY ALLOCATION (DMA)

QUESTION:
Write a program to allocate memory locations for an array of ‘int’ type using ‘new‘ operator.

The program should input values for the length of array during program execution. Enter values

into array and find maximum values of an array.

Note: You have to release the memory using delete operator before the end of program.
 
 


CODE:

#include <iostream>
using namespace std;
int main(){
int size;
cout<<"Enter the size of the array\n";
cin>>size;
int *ptr = new int[size];
cout<<"\nEnter the values in the array\n\n";

for(int i=0;i<size;i++){
cin>>ptr[i];
}
int max = ptr[0];
for(i=0;i<size;i++){
 if(ptr[i] > max){
    max = ptr[i];
 }

}
cout<<"Maximum value of array is : "<<max<<endl<<endl;
delete [] ptr;
ptr = NULL;

 return 0;
}