Use the FinalStarter.c and fill in the missing functions: //your functions to im
ID: 3904729 • 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
Below is your code..
void countEven(int array[], int size)
{
int i, count=0;
for(i=0; i<size; i++){
if(array[i]%2==0)
count++;
}
printf("Count of Even number is: %d ", count);
}
int removeNum(int array[], int position)
{
int i;
int size = CAP;
for(i=position; i<size; i++){
array[i] = array[i+1];
}
size--;
return size;
}
int insertNum(int array[], int num, int position)
{
int i;
int size = CAP;
for(i=size-1; i>position; i--){
array[i+1] = array[i];
}
array[position] = num;
size++;
return size;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.