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

Coding Questions

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

Coding Questions

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

1) Pattern printing

// Input:- Row = 6 Column=5

// Output:=

/*

* * * * *
* @ @ @ *
* @ @ @ *
* @ @ @ *
* @ @ @ *
* * * * *

*/

#include <stdio.h>
void Display(int iRow, int iCol)

{
int i = 0, j = 0;

for (i = 1; i <= iRow; i++)


{

for (j = 1; j <= iCol; j++)


{
if ((i == 1) || (i == iRow) || (j == 1) || (j == iCol))
{
printf("*\t");
}
else
{
printf("@\t");
}
}
printf("\n");
}
}

int main()
{

int iValue1 = 0;
int iValue2 = 0;

printf("Please number of rows :\n");


scanf("%d", &iValue1);

printf("Please number of Columns :\n");


scanf("%d", &iValue2);

Display(iValue1, iValue2);

return 0;
}
2) ////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////
// Write a program which accept string from user and accept one character. Return
index of last occurrence of that character.
//
// Input:
//
// "Marvellous Multi OS"
//
// M
//
// Output: 11
//
///////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
#include <iostream>
using namespace std;

int chkchar(char *str, char ch)


{
int ipos = 0;

int iCnt = 0;

while (*str != '\0')


{
iCnt++;

if (*str == ch)
{
ipos = iCnt;
}

str++;
}

return ipos - 1;
}

int main()
{
int iRet = 0;
char Arr[20];
char cValue = '\0';

cout << "Enter string\n";


cin.getline(Arr, 20);

cout << "Enter charecter:\n";


cin >> cValue;

iRet = chkchar(Arr, cValue);


cout << "Charecter location is\n"
<< iRet;

return 0;
}

3) Write recursive program which accept number from user and return its product of
digits
//input- 523
// output- 30

#include <iostream>
using namespace std;

int ProductR(int iNo)


{
static int iMult = 1;
int iDigit = 0;

if (iNo != 0)
{
iDigit = iNo % 10;
iMult = iMult * iDigit;
iNo = iNo / 10;

ProductR(iNo);
}
return iMult;
}

int main()
{
int iValue = 0;
int iRet = 0;

cout << "Enter number: ";


cin >> iValue;

iRet = ProductR(iValue);
cout << "Product is: " << iRet << endl;

return 0;
}

4) find the maximum value in an array for different data types (e.g., integers,
floats, or doubles) hint : use generic template function

#include <iostream>
using namespace std;

// Template function to find the maximum element in an array


template <typename T>
T findMax(T arr[], int size) {
T maxVal = arr[0]; // Assume the first element is the maximum

// Traverse through the array and find the maximum value


for (int i = 1; i < size; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}

return maxVal; // Return the maximum value found


}

int main() {
// Test case 1: Finding the maximum value in an array of integers
int intArr[] = {3, 5, 7, 2, 8, 1};
int intSize = sizeof(intArr) / sizeof(intArr[0]);
cout << "Maximum value in integer array: " << findMax(intArr, intSize) << endl;

// Test case 2: Finding the maximum value in an array of floats


float floatArr[] = {3.5f, 5.2f, 7.1f, 2.8f, 8.0f, 1.3f};
int floatSize = sizeof(floatArr) / sizeof(floatArr[0]);
cout << "Maximum value in float array: " << findMax(floatArr, floatSize) <<
endl;

// Test case 3: Finding the maximum value in an array of doubles


double doubleArr[] = {3.55, 5.25, 7.11, 2.88, 8.00, 1.33};
int doubleSize = sizeof(doubleArr) / sizeof(doubleArr[0]);
cout << "Maximum value in double array: " << findMax(doubleArr, doubleSize) <<
endl;

return 0;
}

You might also like