Use the FinalStarter.c and fill in the missing functions: //your functions to im
ID: 3903287 • Letter: U
Question
Use the FinalStarter.c and fill in the missing functions:
//your functions to implement
//this function counts the number of even numbers in the list at a given time
void countEven(int[], int size);
/*This function removes a number from the given position. You must pack the array from the bottom and return the new size. So, if one number is removed the size will decrement by 1 and so on.*/
int removeNum(int[], int position);
/*This function inserts the number, num, at the given position, and returns the new size. So, if the number 5 was added to the list, it would return the size incremented by 1*/
int insertNum(int[], int num, int position);
This is the missing program for part 2;
/*This program has a function that will generate a list of integers using the rand function.
Your job is to fill the implementation functions.*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//constants and function prototypes
const int CAP = 10;
void buildList(int[], int size);
void printList(int[], int size);
//your functions to implement
//this function counts the number of even numbers in the list at a given time
void countEven(int[], int size);
/*This function removes a number from the given position. You must pack the array from the bottom and return the new size. So, if
1 number is removed the size will decrement by 1 and so on.*/
int removeNum(int[], int position);
/*This function inserts the number, num, at the given position, and returns the new size. So, if the number 5 was added to the list, it would
return the size incremented by 1*/
int insertNum(int[], int num, int position);
int main()
{
//DO NOT CHANGE MAIN
int list[10], size = CAP;
buildList(list, size);
printList(list, size);
countEven(list, size);
size = removeNum(list, 4);
size = removeNum(list, 5);
printList(list, size);
size = insertNum(list, 10, 1);
size = insertNum(list, 18, 0);
printList(list, size);
return 0;
}
//function to build list. DO NOT CHANGE THIS
void buildList(int list[], int size)
{
srand(time(NULL));
for (int i = 0; i < size; i++)
{
list[i] = rand() % 100;
}
}
//function to print list. DO NOT CHANGE THIS
void printList(int list[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
}
//implement the missing functions here.
Explanation / Answer
counteven() -
void countEven(int []a, int size)
{
//initailize a variable "res" to 0
int res=0,i;
for(i=0;i<size;i++)
{
//check for an even element
if(a[i]%2==0)
res++; //If even element is found increment the count by 1
}
printf("number of even elements in array = %d",res);
}
removenum() -
int removeNum(int []a, int position);
{
int i;
//loop from starting of the index from where element is to be removed
//loop till the size of array
for(i=position;i<size-1;i++)
{
a[i]=a[i+1]; //shift every element to its left
}
size--; //decrement size
return size;
}
insertNum() -
int insertNum(int []a, int num, int position);
{
//Check for the capacity of the array
if(size>CAP)
{
printf("element cannot be added"); //Display error message if overflow occurs
return -1;
}
int i;
size++; //increment size
//loop from end of the array
//loop till the index where element is to be inserted
for(i=size;i>position;i--)
{
a[i]=a[i-1]; //shift every element to its right
}
//place the value to be inserted
a[position]=num;
return size;
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.