For this exam review lab exercise, you will develop a program that fixes faulty data in a one-dimensional array of integers. Faulty data is represented by a value of 9999, and should be replaced by the average of the two neighboring elements, where integer casting should be applied to non-integer averages (to keep the data stored as integers). If the first or last element is faulty, it should be replaced by the value of its only neighboring element. If two adjacent neighboring elements are faulty anywhere in the array, a message should be printed to screen stating that the data cannot be fixed. The programming tasks have been segmented into levels. It is strongly recommended that you complete all lower level tasks before moving on to a higher level.
A code template has been provided for you that reads in data from the user and prints out the inputted data to screen. DO NOT MODIFY THE TEMPLATE CODE, INSTEAD ONLY ADD FUNCTIONALITY FOR THE LEVEL 1-3 TASKS AS DESCRIBED BELOW.
Programming Tasks
Level 0 Tasks
(50 points)
Level 1 Tasks
(25 points)
No Faulty Data!
OR
Fixing Faulty Data...
Level 2 Tasks
(15 points)
Level 3 Tasks
(10 points )
Adjacent elements are faulty. Data cannot be fixed!
Sample Output
Here are some sample outputs for your reference:
A code that achieves Level 1 Tasks:
Input:
12 2 9999 17 9999 -2 22 9999 4 11 9999 8 6
Output:
Enter size of data: Enter data: Inputted data: 2 9999 17 9999 -2 22 9999 4 11 9999 8 6 Fixing Faulty Data... Fixed Data: 2 9 17 7 -2 22 13 4 11 9 8 6
A code that achieves Level 2 Tasks:
Input:
11 9999 17 9999 -2 22 9999 4 11 9999 8 6
Output:
Enter size of data: Enter data: Inputted data: 9999 17 9999 -2 22 9999 4 11 9999 8 6 Fixing Faulty Data... Fixed Data: 17 17 7 -2 22 13 4 11 9 8 6
A code that achieves Level 3 Tasks:
Input:
10 9999 9999 -2 22 9999 4 11 9999 8 6
Output:
Enter size of data: Enter data: Inputted data: 9999 9999 -2 22 9999 4 11 9999 8 6 Fixing Faulty Data... Adjacent elements are faulty. Data cannot be fixed!
A code that achieves Level 3+ Tasks:
Input:
10 9999 -2 22 9999 4 11 8 9999 7 9999
Output:
Enter size of data: Enter data: Inputted data: 9999 -2 22 9999 4 11 8 9999 7 9999 Fixing Faulty Data... Fixed Data: -2 -2 22 13 4 11 8 7 7 7 * * * *
Here is the template:
#include <stdio.h>
#include <stdbool.h>
//readData() function provided to read in data from user input
(DO NOT REMOVE or MODIFY)
void readData(int data[], int size) {
for (int i=0; i<size; ++i) {
scanf("%d",&data[i]);
}
}
//printData() function provided to print integer data
array,
// reseving 4 spots for each int and a space in between (DO NOT
REMOVE or MODIFY)
void printData(int data[], int size) {
for (int i=0; i<size; ++i) {
printf("%-4d ",data[i]);
}
printf("\n");
}
//LEVEL 1
bool checkData(int data[], int size, int badValue){
int i;
for(i=0;i<size;i++)
{
if(data[i] == badValue)
{
return false;
}
}
return true;
}
//developed for LEVEL 1, and extended in LEVELS 2 and 3
int fixData(int data[], int size, int badValue){
int i;
char ast[size];
for(i=0;i<size;i++)
ast[i]=' ';
for(i=0;i<size;i++)
{
if(data[0]==badValue && data[1]!=badValue)
{
data[0]=data[1];
ast[0]='*';
}
else if(data[size-1]==badValue && data[size-2] !=
badValue)
{
data[size-1]=data[size-2];
ast[size-1]='*';
}
else if(data[i]==badValue && data[i+1]==badValue)
return -1;
else
if(data[i]==badValue)
{
data[i]=(data[i-1]+data[i+1])/2;
ast[i]='*';
}
}
printData(data,size,ast);
return 0;
}
int main() {
//start of provided code to read in data and print it
int n;
int temps[100];
printf("Enter size of data: ");
scanf("%d",&n);
printf("\nEnter data: ");
readData(temps,n);
printf("\nInputted data: \n");
printData(temps,n);
//end of provided code to read in data and print it
//DEVELOP YOUR main() CODE HERE
return 0;
}
If you have any doubts, please give me comment...
#include <stdio.h>
#include <stdbool.h>
//readData() function provided to read in data from user input (DO NOT REMOVE or MODIFY)
void readData(int data[], int size) {
for (int i=0; i<size; ++i) {
scanf("%d",&data[i]);
}
}
//printData() function provided to print integer data array,
// reseving 4 spots for each int and a space in between (DO NOT REMOVE or MODIFY)
void printData(int data[], int size) {
for (int i=0; i<size; ++i) {
printf("%-4d ",data[i]);
}
printf("\n");
}
void printAst(char ast[], int size){
for (int i=0; i<size; ++i) {
printf("%-4c ",ast[i]);
}
printf("\n");
}
//LEVEL 1
bool checkData(int data[], int size, int badValue){
int i;
for(i=0;i<size;i++)
{
if(data[i] == badValue)
{
return false;
}
}
return true;
}
//developed for LEVEL 1, and extended in LEVELS 2 and 3
int fixData(int data[], int size, int badValue){
int i;
char ast[size];
for(i=0;i<size;i++)
ast[i]=' ';
for(i=0;i<size;i++)
{
if(data[0]==badValue && data[1]!=badValue)
{
data[0]=data[1];
ast[0]='*';
}
else if(data[size-1]==badValue && data[size-2] != badValue)
{
data[size-1]=data[size-2];
ast[size-1]='*';
}
else if(data[i]==badValue && data[i+1]==badValue)
return -1;
else
if(data[i]==badValue)
{
data[i]=(data[i-1]+data[i+1])/2;
ast[i]='*';
}
}
printf("Fix Data:\n");
printData(data,size);
printAst(ast, size);
return 0;
}
int main() {
//start of provided code to read in data and print it
int n;
int temps[100];
printf("Enter size of data: ");
scanf("%d",&n);
printf("\nEnter data: ");
readData(temps,n);
printf("\nInputted data: \n");
printData(temps,n);
//end of provided code to read in data and print it
if(!checkData(temps, n, 9999)){
printf("Fixing Faulty Data...\n");
if(fixData(temps, n, 9999)==-1)
printf("Adjacent elements are faulty. Data cannot be fixed!\n");
}
//DEVELOP YOUR main() CODE HERE
return 0;
}
For this exam review lab exercise, you will develop a program that fixes faulty data in...
//This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...
//This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...
//This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...
4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...
Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...
Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /* copy_positive(A, Aout, size) * Given an array A, which will have the provided size, copy all positive * (non-negative/non-zero) elements of A into the provided output array Aout. * Return the size of the resulting array. */ /* (your code from below would be placed here) */ void print_array(int arr[], int n){ for( int i = 0; i < n; i++ ) printf("%d ",...
I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...
I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...