Part 1: Write pseudo code to do the following. Your pseudo code or algorithm mus
ID: 3902084 • Letter: P
Question
Part 1:
Write pseudo code to do the following. Your pseudo code or algorithm must follow all the rules of an algorithm. It must be written in steps. No paragraphs. All conditions and iterations must be clearly specified. You must use loops and functions to do this. The steps in the pseudo code must be very clear without ambiguity. Please submit a separate txt file for this part.
1. To remove numbers from an array given the array and the position of the number to remove. You should not replace the element with 0. If your original array was {34, 12, 56, 18, 20, 16, 8, 9, 10, 11}, after removing the element from position 0, your array should print {12, 56, 18, 20, 16, 8, 9, 10 11}.
2. To insert numbers from an array given the array, the number to insert, and the position where the number should be inserted. If your original array was {34, 12, 56, 18, 9, 10, 11}, after inserting the number 15 at position 1, your array should print {15, 34, 12, 56, 18, 9, 10 11}. You should make sure that size does not exceed capacity when inserting.
Part 2:
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
PART 1 -
We assume that array indexing starts from 0 and size of array be stored in variable "size" and capacity be "cap"
Pseudocode -
1) Get index of element to be removed in variable named "ind"
Run a loop starting from "ind" till "size"-1 do
shift all elements left by 1 position a[i] = a[i+1]
done
decrease "size" by 1
2) Get index where the element is to be inserted in variable named "ind"
Check whether "size" = "cap" if so then return false indicating that element cannot be added
else
increase "size" by 1
Now loop from "size" till "ind" and move each element to right do-
a[i] = a[i-1]
done
Now insert the element to be added at a["ind"]
PART 2 -
Below are the functions which you demanded-
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);
}
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;
}
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 ;
}
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.