Function (Passing arrays to Function) + Recursion
Question
Function (Passing arrays to Function) + Recursion
1) Write a program that reads a line from the user until the user presses enter key (n). Store all the words of a line into a multidimensional dynamic array. Then pass this array to a function Sort Stringil, which will sort all the words by word length Le longest word will be placed at a first index, then the second longest word will be placed at a second index, and so on. Finally return the sorted list of words back to the main function, where all the words will be printed onto the screen as one word per line. Assume that the line contains all unique words
Sample Input/Output
Input:
I hope my Grade in PF will be A+.
Output
Grade
hope
will
A+
be
in
my
2) Write a program that takes a string (Roll No Name) from the user and then reverse using recursion example: String 20k-1111 Sanalqbal
3) Print a Fibonacci series up to ‘N’ numbers using recursion. Where N should be taken from the user
4) Write a program to shift every element of an array to circularly right
Input12345
Output: 51234
You have to u perform 3 circular night rotations and have to pass the input array to a function.
5) Write a program in which Student enter the marks of ‘N’ subjects out of 100, save these
marks in an array, Pass this array to a function calculate Result) and assign grades accordingly.
Summary
The given string will be divided into words, which will be sorted in decreasing length order.
A recursive function is used to reverse a string.
The Fibonacci sequence can be found up to n numbers.
The array elements are shifted by one to the right.
A student’s grade is determined based on his or her grades.
Explanation
1) The user enters input with whitespaces, the string is split based on the whitespaces, and the words are provided to a sort function, which sorts the words in decreasing order of word length.
2) The reverse function is written, which prints the string provided to it backwards and is implemented in a recursive manner.
3) The recursive function “fib” is defined to return the nth element in the Fibonacci series.
4) First, the array elements are declared, and the last element is saved in a temp variable; then, starting with the last element of the array, each element is replaced with its predecessor. Finally, the 0th index stores the last piece.
5) The student’s grades are supplied to the calculateResult() function, which finds the sum of the marks and calculates the average, from which the grade is estimated.
Code
#include <stdio.h> #include <string.h> /* function to sort the words based on descending order of length */ char** SortString(char s[20][20], int cc) { int length[cc]; for(int i=0;i<cc;i++) length[i]=strlen(s[i]); for(int i=0;i<cc;i++){ for(int j=0;j<cc-1-i;j++){ if(length[j]<=length[j+1]){ int temp=length[j]; length[j]=length[j+1]; length[j+1]=temp; char temp1[10]; strcpy(temp1,s[j]); strcpy(s[j],s[j+1]); strcpy(s[j+1],temp1); } } } return s; } /* driver code */ int main() { char c[200]; printf("Input:\n"); scanf("%[^\n]%*c", c); char s[20][20]; int cc=0; int j=0; /* to split the input string into words */ for(int i=0;i<=strlen(c);i++){ if(c[i]==' ' || c[i]=='\0'){ s[cc][j]='\0'; cc++; j=0; } else{ s[cc][j]=c[i]; j++; } } SortString(s,cc); printf("\nOutput:\n"); for(int i=0;i<cc;i++){ printf("%s\n",s[i]); } }
Output
Code 2
#include <stdio.h> #include <string.h> void reverse(char s[100], int cc) { if(cc==-1) return; cc--; printf("%c", s[cc]); reverse(s, cc); } int main() { char s[100]; int cc; printf("Enter the string:\n"); scanf("%[^\n]%*c",s); cc=strlen(s); printf("\nOutput:\n"); reverse(s, cc); return 0; }
Output
Code 3
#include <stdio.h> int fib(int n) { if(n==1) return 0; else if(n==2) return 1; return fib(n-1) + fib(n-2); } int main() { int n=10; for(int i=1;i<=n;i++){ printf("%d ",fib(i)); } }
Output
Code 4
#include <stdio.h> int main() { int arr[5]={1,2,3,4,5}; int temp=arr[4]; for(int i=4;i>=1;i--) { arr[i]=arr[i-1]; } arr[0]=temp; printf("Output:\n"); for(int i=0;i<5;i++) printf("%d ",arr[i]); }
Output
Code 5
#include <stdio.h> /* function to calculate the grade */ void calculateResult(int marks[], int n) { int sum=0; for(int i=0;i<n;i++) sum=sum+marks[i]; float avg=sum/(float)n; if(avg>=91) printf("Grade: S"); else if(avg>=85) printf("Grade: A+"); else if(avg>=75) printf("Grade: A"); else if(avg>=65) printf("Grade: B"); else if(avg>=60) printf("Grade: C"); else if(avg>=50) printf("Grade: D"); else printf("Fail"); } int main() { int n; printf("Enter N: "); scanf("%d",&n); int marks[n]; printf("Enter the marks of %d subjects:\n",n); for(int i=0;i<n;i++) scanf("%d",&marks[i]); calculateResult(marks, n); }
Output
Also, read check the python code below that doesn’t work properly, adjust it, and explain?