0% found this document useful (0 votes)
26 views4 pages

C++ Programming Exercises Pointers

The document contains 6 questions related to C++ programming. Question 1 declares pointer variables for a double and double array. Question 2 inputs and displays a value to the double pointer variable. Question 3 initializes the double array values to 1.0. Question 4 finds the maximum value in an integer array. Question 5 declares and initializes a structure variable. Question 6 counts the frequency of letters in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

C++ Programming Exercises Pointers

The document contains 6 questions related to C++ programming. Question 1 declares pointer variables for a double and double array. Question 2 inputs and displays a value to the double pointer variable. Question 3 initializes the double array values to 1.0. Question 4 finds the maximum value in an integer array. Question 5 declares and initializes a structure variable. Question 6 counts the frequency of letters in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Questions 1 - 3

//Qs 1 - 3

#include <iostream>
#include <stdlib.h>
using namespace std;

int main (){

//Q1 - declare pointer variables d_var and d_array


double *d_var, *d_array;
d_var=new double;
d_array=new double[10];

//Q2 - input and display value for d_var from console


cout <<"Enter a value ";
cin >> *d_var;
cout << "Value entered " << *d_var;

//Q3 - initialize d_array values to 1.0


for (int i = 0; i < 10; i++) {
d_array[i] = 1.0;
}

//delete to de-allocate memory


delete []d_array;
delete d_var;

return 0;
}

Question 4
#include <iostream>
#include <stdlib.h>
using namespace std;

void findMax(int array[],int size,int*max)


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

int main ()
{
int num,max;
cout << "Input total number of elements: " << endl;
cin>>num;
int *array = new int[num];

for(int i=0;i<num;i++){
cout<<"Number "<<i+1<<" : ";
cin>>array[i];
cout<<"\n";
}

for(int i=0;i<num;i++){
cout<<array[i]<<" ";
}

findMax(array,num,&max);
cout<<"\nThe Largest Element is: "<<max;

Question 5

#include <iostream>
using namespace std;
struct Timer{
int hours;
int minutes;
};

int main () {
struct Timer *timePtr = new struct Timer;

(*timePtr).hours=10;
(*timePtr).minutes=20;

cout <<"Timer = "<< (*timePtr).hours <<" hours "<< (*timePtr).minutes << "
minutes ";

return 0;

Question 6

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int * letter_f(char s[]);


int main(){
int *frequency;
char *s = new char[100];
frequency = new int[26];

cout << " Enter a string : ";


cin.getline(s, ' ');

frequency = letter_f(s);
cout << "\nLetter frequency :"<< endl;

for(int i = 0; i<26; i++){


if(frequency[i] != 0)
cout << " "<< static_cast<char>(i + 'a') << " " << frequency[i] << endl;
}
system("pause");
return 0;
}

int *letter_f(char s[]){


int *arrayOcc;
arrayOcc = new int[26];
int count;

for(int i = 0; i <26; i++){


count = 0;
for(int j=0; j <strlen(s); j++){
if (int('a') + i ==int(tolower(s[j])))
count++;
}
arrayOcc[i] = count;
}
return arrayOcc;
}

You might also like