program in C laguage the first problem requires that the student take in charact
ID: 3870057 • Letter: P
Question
program in C laguage
the first problem requires that the student take in character input from a prompt "> " on a character by character basis. There should be no more than 80 characters in a single line of input. You might look at the <stdio.h> library function getchar() to help with this. Then, output the resulting string in the line in reverse. Note that a string must have a '' at the end of the character array.
The second problem is to determine some basic statistics on set of integers. The required statistics are the: mean, mode, min, max, and median. The set of integers to use in your array is: 36, 14, 9, 24, 16, 25, 9, 13, 2, 28. Build a table like the previous homework to show your results. You should be able to find definitions for these statistical terms on the web.
Explanation / Answer
Question 1.
#include <stdio.h>
#include <string.h>
int main()
{
char str[80], ch,temp;
int i = 0;
int k,j;
int count=0;
printf("Enter name: ");
while(ch != ' '&& count<80) // terminates if user hit enter
{
ch = getchar();
str[i] = ch;
i++;
count++;
}
str[i] = ''; // inserting null character at end
k = 0;
j = strlen(str) - 1;
while (k < j) {
temp = str[k];
str[k] = str[j];
str[j] = temp;
k++;
j--;
}
printf("Reverse of entered string is %s ",str);
return 0;
}
Question 2.
#include<stdio.h>
double arr[10]={36,14,9,24,16,25,9,13,2,28}; // given array
int n=10;
int findmean(void) // to find mean value
{
double sum=0.0,mean=0.0;
int j;
for(j=0;j<n;j++)
{
sum+=arr[j];
}
mean=sum/n;
printf(" Mean=%.2lf",mean);
}
int sort(void) // to sort array
{
double temp;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>=arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void max_min(void) // to find min and max value
{
double max;
max=arr[n-1];
printf(" Max=%.1lf",max);
double min;
min=arr[0];
printf(" Min=%.1lf",min);
}
void median(void) //to find median
{
double res;
double a,b;
a=arr[n/2];
b=arr[n/2-1];
if(n%2==0)
{
res=(a+b)/2;
}
else
res=arr[n/2];
printf(" Median is %.2lf",res);
}
int mode() // to find mode
{
int n=10;
int cnt=0,i,pos;
double mode1;
int m;
for(i=0;i<n;i++)
{
if(arr[i]==arr[i+1])
{
mode1=arr[i];
pos=i;
cnt++;
}
}
m=n-i;
while(m!=0)
{
mode();
}
printf(" Mode=%.1lf",mode1);
return m;
}
int main()
{
findmean();
sort();
median();
mode();
max_min();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.