Must use C code!, Must create arrays, Must not create special functions such as
ID: 3598512 • Letter: M
Question
Must use C code!, Must create arrays, Must not create special functions such as void (), Must create with least amount of complexity.
program must ask user to randomly set a size for the arrray then Given a set of number to find the required output related to 0s from it.
Do not create program only work for the example below, it must work for any case
For example:(again do not create program only work for the scenario below it just a example)
4, 0, 0, 0, 0, 8, 0, 0, 1, 0
Given the example above
output must shown following:
size of array: 10
order of length: 1 2 3
length of zero: 4 2 1 -1 ( must correspond to the order each length appear then use -1 as sentinel to stop counting of the length)
the amount of zero: 7
the longest length is: 4
the shortest length is 1
the average length is 2.3
AGAIN! Creat a program work universally not just the given example like above!!!
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,length,min,max,count;
double sum;
int data[100]; //Assuning max size to be 100
int order[10];
printf("Enter size of array :");
scanf("%d",&n);
for (i=0; i<n; i++){
scanf("%d",&data[i]);
}
count = 0;
length = 0;
int flag = 0;
for (i=0; i<n; i++){
if (data[i] == 0){
if (flag == 0){
flag = 1;
length = 1;
}
else {
if (i > 0){
if (data[i-1] == 0)
length++;
}
}
}
if (data[i] != 0){
if (flag == 1){
order[count] = length;
length = 0;
count++;
flag = 0;
}
}
}
if (flag == 1){
order[count] = length;
count++;
}
printf("Size of array : %d ",n);
printf("order of length:");
for (i=0; i<count; i++){
printf("%d ",i+1);
}
printf(" ");
printf("length of zero: ",count);
for (i=0; i<count; i++){
printf("%d ",order[i]);
}
printf("-1 ");
sum = 0;
max = 0;
min = 0; // Arbitrarily large value
for (i=0; i<count; i++){
if (i == 0){
max = order[i];
min = order[i];
}
else {
if (order[i] > max)
max = order[i];
if (order[i] < min)
min = order[i];
}
sum = sum + order[i];
}
printf("the amount of zero: %d ", (int)sum);
printf("the longest length is : %d ", max);
printf("the shortest length is : %d ", min);
printf("the average length is : %lf ", sum/count);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.