I have to build off of this current program in C and I am very confused on this
ID: 3917659 • Letter: I
Question
I have to build off of this current program in C and I am very confused on this assignment.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll(int x){
if (x>50 || x < 1){
return 4;
}
else{
return (rand()%(x+1));
}
}
int main(int argc, char const *argv[])
{
int A[50];
srand(time(NULL));
int k,i,sum=0,countOf7=0,consecutiveSameNums=0;
for ( k = 0; k < 50; ++k ){
A[k] = roll(10);
}
printf("first 3 values ");
for ( k = 0; k < 3; ++k )
printf("%d ", A[k]);
printf(" ");
for (i = 0; i < 50; ++i)
{
sum=sum+A[i];
}
printf("Sum : %d ",sum );
for (i = 0; i < 50; ++i)
{
if (A[i]==7)
{
countOf7++;
countOf7++;
}
}
printf("7 rolled %d times ",countOf7 );
for (i = 0; i < 49; ++i)
{
if (A[i]==A[i+1])
{
consecutiveSameNums++;
}
}
printf("%d times two consecutive same numbers rolled ",consecutiveSameNums$
return 0;
}
Assignment:
For the quiz, roll a 10-sided dice 50 times
Write each of these functions, then call appropriate so solve the requests problems
Print the values in an array: void printArray( int A[], int N )
A is the array of integers, N is the number of items to print
Find the sum of values in an array: int sumArray( int A[], int N )
Frequency of value in array: int freqArray( int A[], int N[], int T)
A is array, N is number of item, T is target value
Utility function – make sure you have this function:
int roll( int )
Purpose: roll a dice, return the value
Params: how sides on the dice
If # of sides is outside 1 to 50 range, return 4
Additional functions to write & test:
Write the functions, test them, you’ll submit to the quiz
int isVowel( char )
Purpose: determine if character is a vowel, return 1 for yes, 0 for no
Note: y is note a vowel, not even sometimes :)
Params: a character
char toGrade( int ) [ABCDF]
Purpose: convert numeric score to letter grade, standard 90-80-70…
Params: integer representing score
If score is outside 0 to 100 legal range, return ?
int fullCartons( int )
Purpose: how egg cartons (dozen) can be filled
Params: number of eggs
void zigzag( int )
Purpose: make 1 zig-zag bump
Params: height of the zig
This is a zigzag of size 4
(‘-‘ to illustrate but must be ‘ ‘ in your code)
#
-#
--#
---#
--#
-#
#
Explanation / Answer
here is your code : -------------->>>>>>>>>>>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll(int x){
if (x>50 || x < 1){
return 4;
}
else{
return (rand()%x+1);
}
}
void printArray( int A[],int N){
int i;
for(i= 0;i<N;i++){
printf("%d ",A[i]);
}
}
int sumArray(int A[],int N){
int i;
int val = 0;
for(i= 0;i<N;i++){
val += A[i];
}
return val;
}
int freqArray(int A[],int N,int T){
int i;
int val = 0;
for(i= 0;i<N;i++){
if(T == A[i]){
val++;
}
}
return val;
}
int isVowel(char ch){
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
return 1;
}
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
return 1;
}
return 0;
}
char toGrade(int grade){
if(grade >= 90){
return 'A';
}else if(grade >= 80){
return 'B';
}else if(grade >= 70){
return 'C';
}else if(grade >= 60){
return 'D';
}else{
return 'F';
}
}
int fullCartons(int numEgg){
return numEgg/12;
}
void zigzag(int num){
int i,j;
for(i = 0;i<num;i++){
for(j = 0;j<i;j++){
printf(" ");
}
printf("# ");
}
for(i = i-2;i>=0;i--){
for(j = 0;j<i;j++){
printf(" ");
}
printf("# ");
}
}
int main(int argc, char const *argv[])
{
int A[50];
srand(time(NULL));
int k,i,sum=0,countOf7=0;
char check;
for ( k = 0; k < 50; ++k ){
A[k] = roll(10);
}
printf(" Printing the Rolled Array ");
printArray(A,50);
sum = sumArray(A,50);
printf("Sum : %d ",sum );
countOf7 = freqArray(A,50,7);
printf("7 rolled %d times ",countOf7 );
while(1){
printf(" Enter a character to check it is vowel or not : ");
scanf("%c%*c",&check);
i = isVowel(check);
if(i == 1){
printf(" Is A Vowel.");
}else{
printf(" Is Not A Vowel.");
}
printf(" Do you want to check more(y/n)? ");
scanf("%c%*c",&check);
if(check == 'y' || check == 'Y'){
continue;
}else{
break;
}
}
printf(" Enter A number to print Zig Zag : ");
scanf("%d",&i);
zigzag(i);
printf(" Enter A grade to see char equivalent : ");
scanf("%d",&i);
printf(" grade = %c",toGrade(i));
return 0;
}