0% found this document useful (0 votes)
33 views

QUESTION #1 Solution

This document contains C++ code to solve two problems. The first problem involves reversing the words in a sentence by reversing each word individually and reversing the order of the words. The second problem finds the intersection of two arrays by comparing elements and adding matches to a new array.

Uploaded by

Waqas Maan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

QUESTION #1 Solution

This document contains C++ code to solve two problems. The first problem involves reversing the words in a sentence by reversing each word individually and reversing the order of the words. The second problem finds the intersection of two arrays by comparing elements and adding matches to a new array.

Uploaded by

Waqas Maan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

QUESTION #1 Solution:

#include<iostream>
using namespace std;
void ReverseSentence( char * a);
void ReverseWord(char *p, int min_size, int max_size);
int main()
{
char input[100];
cin.getline(input,100);
char *q=input;
ReverseSentence(q);
cout<<input<<endl;
}

void ReverseWord(char *p, int min_size, int max_size)
{
char temp;
int len=max_size-min_size;
for(int i=0; i<len/2; i++)
{
temp=p[i+min_size];
p[i+min_size]=p[max_size-i-1];
p[max_size-i-1]=temp;
}
}

void ReverseSentence( char * a)
{
int i=0;
int max_size=0;
int min_size=0;
while(true)
{
if(a[i]==' '|| a[i]=='.')
{
min_size=max_size;
max_size=i;
ReverseWord(a,min_size, max_size);
if(a[i]=='.')
break;
}
i++;
}
}





QUESTION #2 SOLUTION:
#include<iostream>
using namespace std;
void getdata(int * & a, int & size);
int intersection( int *a1, int *a2, int * & a3, int s1, int s2);
int main()
{
int * arr1, *arr2, *arr3;
int size1, size2, size3;
cout<<"First Array : "<<endl;
getdata(arr1,size1);
cout<<"Second Array : "<<endl;
getdata(arr2,size2);
size3=intersection(arr1,arr2,arr3,size1,size2);
cout<<"Third Array :"<<endl;
for( int i=0; i<size3; i++)
cout<<arr3[i]<<" ";
}
void getdata(int * & a, int & size)
{
cout<<"Enter the size of array :";
cin>>size;
a= new int [size];

cout<<endl<<"Enter the elements of array : "<<endl;
for( int i=0; i<size; i++)
cin>>a[i];
}
int intersection( int *a1, int *a2, int * & a3, int s1, int s2)
{
int count=0;
for( int i=0; i<s1; i++)
for( int j=0; j<s2; j++)
if(a1[i]==a2[j])
{
count++;
break;
}
a3= new int [count];
int ind=0;
for( int i=0; i<s1; i++)
for( int j=0; j<s2; j++)
if(a1[i]==a2[j])
{
a3[ind]=a1[i];
ind++;
break;
}
return count;
}

You might also like