Please help me with these thanks Problem 1 Write a non recu
Please help me with these thanks!!
Problem 1 Write a non - recursive function that , given an array of integers, will return the maximum element.
Problem 2 Write a recursive function that , given an array of integers, will return the maximum element.
Problem 3 Modify the function s in Problems 1 and 2 to be a function template s , so that it will work with arrays of doubles, characters, strings, etc.
Problem 4 Write a function that given an array of strings, it will sort it in ascending order.
Problem 5 Write a recursive function that converts a non - negative integer decimal number to binary.
Problem 6 Write a function that given two dynamic M by N arrays of doubles A and B, will return a dynamic M by N array C whose elements are the sum of the corresponding elements in A and B (i.e., C[i][j]=A[i][j]+B[i][j] ). double** sumArrays (double **A, double **B, int M, int N);
Problem 7 Write a function that g iven a dynamic array of doubles A of size M, will a dynamic array B of size M, where B[0]=A[0], B[1]=A[0]+A[1], B[2]= A[0]+A[1]+A[2], ..., B[M]=A[0]+A[1]+...+A[M]. double* cumulativeSum (double *A, int M)
Solution
1)We can traverse the array and keep track of maximum and element. And finally return the maximum element.
#include <stdio.h>
int findMaximum(int arr[], int low, int high)
{
int max = arr[low];
int i;
for (i = low; i <= high; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
/* Driver program to check above functions */
int main()
{ int arr[] = {1, 30, 40, 50, 60, 70, 23, 20};
int n = sizeof(arr)/sizeof(arr[0]);
printf(\"The maximum element is %d\", findMaximum(arr, 0, n-1));
getchar();
return 0;
}
Method 2 (Binary Search)
#include <stdio.h>
int findMaximum(int arr[], int low, int high)
{
/* Base Case: Only one element is present in arr[low..high]*/
if (low == high)
return arr[low];
/* If there are two elements and first is greater then
the first element is maximum */
if ((high == low + 1) && arr[low] >= arr[high])
return arr[low];
/* If there are two elements and second is greater then
the second element is maximum */
if ((high == low + 1) && arr[low] < arr[high])
return arr[high];
int mid = (low + high)/2; /*low + (high - low)/2;*/
/* If we reach a point where arr[mid] is greater than both of
its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
is the maximum element*/
if ( arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1])
return arr[mid];
/* If arr[mid] is greater than the next element and smaller than the previous
element then maximum lies on left side of mid */
if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
return findMaximum(arr, low, mid-1);
else // when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1]
return findMaximum(arr, mid + 1, high);
}
/* Driver program to check above functions */
int main()
{
int arr[] = {1, 3, 50, 10, 9, 7, 6};
int n = sizeof(arr)/sizeof(arr[0]);
printf(\"The maximum element is %d\", findMaximum(arr, 0, n-1));
getchar();
return 0;
}
AND
C code to get largest element of an array by recursion:
#include<stdio.h>
#define MAX 100
int getMaxElement(int []);
int size;
int main(){
int arr[MAX],max,i;
printf(\"Enter the size of the array: \");
scanf(\"%d\",&size);
printf(\"Enter %d elements of an array: \", size);
for(i=0;i<size;i++)
scanf(\"%d\",&arr[i]);
max=getMaxElement(arr);
printf(\"Largest element of an array is: %d\",max);
return 0;
}
int getMaxElement(int arr[]){
static int i=0,max =-9999;
if(i < size){
if(max<arr[i])
max=arr[i];
i++;
getMaxElement(arr);
}
return max;
}
Sample output:
Enter the size of the array: 5
Enter 5 elements of an array: 1 4 5 6 2
Largest element of an array is: 6
2)
C code to get largest element of an array without recursion:
#include<stdio.h>
#define MAX 100
int getMaxElement(int []);
int size;
int main(){
int arr[MAX],max,i;
printf(\"Enter the size of the array: \");
scanf(\"%d\",&size);
printf(\"Enter %d elements of an array: \", size);
for(i=0;i<size;i++)
scanf(\"%d\",&arr[i]);
max=getMaxElement(arr);
printf(\"Largest element of an array is: %d\",max);
return 0;
}
int getMaxElement(int arr[]){
int i=1,max;
max=arr[0];
while(i < size){
if(max<arr[i])
max=arr[i];
i++;
}
return max;
}
4)
//Write a C program to sort a string array in ascending order.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[20],ch;
int i,j,l;
clrscr();
printf(\"enter any string:\");
gets(st);
l=strlen(st);
/* sorting process */
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(st[j]>st[j+1])
{
ch=st[j];
st[j] = st[j+1];
st[j+1]=ch;
}
printf(\"Sorted string is \ \");
printf(\"%s\ \",st);
getch();
}
5)
/*
* C Program to Convert a Number Decimal System to Binary System using Recursion
*/
#include <stdio.h>
int convert(int);
int main()
{
int dec, bin;
printf(\"Enter a decimal number: \");
scanf(\"%d\", &dec);
bin = convert(dec);
printf(\"The binary equivalent of %d is %d.\ \", dec, bin);
return 0;
}
int convert(int dec)
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}
6)
#include<stdio.h>
#include<conio.h>
void read_arr(int a[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf(\"Enter Element %d %d : \",i,j);
scanf(\"%d\",&a[i][j]);
}
}
}
void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j] = (m1[i][j] + m2[i][j]);
}
}
}
void print_arr(int m[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf(\"%d \",m[i][j]);
}
printf(\"\ \");
}
}
main()
{
int m1[10][10],m2[10][10],m3[10][10],row,col;
clrscr();
printf(\"Enter number of rows :\");
scanf(\"%d\",&row);
printf(\"Enter number of colomns :\");
scanf(\"%d\",&col);
read_arr(m1,row,col);
read_arr(m2,row,col);
add_arr(m1,m2,m3,row,col);
print_arr(m3,row,col);
getch();
}







