Question

For this exam review lab exercise, you will develop a program that fixes faulty data in...

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)

  • Simply ensure you submit a code that compiles and runs without any errors and that the functionality of the code template has not been modified. Beyond that, you don't need to do anything else to earn these points. (To be clear, submit the template right now, and you should get at least 50 points.)


Level 1 Tasks

(25 points)

  • Write the checkData() function, which takes in as parameters an integer data array, the array size, and another integer badValue. The function should return true if there are no elements in the array that contain badValue. The function should return false if the array stores badValue at any element in the array.
  • Call checkData() from main() to check for faulty data in the array temps. Depending on the result, print to screen either
No Faulty Data!

OR

Fixing Faulty Data...
  • Write the fixData() function, which has the same parameters as checkData(). The function should fix the data by replacing all elements that are badValue with the average of the two neighboring elements. For Level 1, you can assume that the first and last elements do not contain badValue and that no two consecutive elements contain badValue. The function should return the number of elements that originally stored badValue but have now been fixed.
  • Call fixData() from main() to fix the data in temps and print the updated array to screen using the provided printData() function.


Level 2 Tasks

(15 points)

  • Update fixData() for cases where badValue is stored in the first or last element. In these cases, replace the the faulty data with the only neighboring element. For Level 2, you can still assume that no two consecutive elements contain badValue.


Level 3 Tasks

(10 points )

  • Update fixData() for cases where badValue is stored in two consecutive elements. In these cases, return -1 to main(), where you the following message should be printed to screen (and then the program immediately terminates):
Adjacent elements are faulty. Data cannot be fixed!
  • For this final (Level 3+) task, print an asterisk (i.e. * ) underneath the elements of the fixed array (printed to screen in the last Level 1 Task) for all elements that originally were faulty, but have now been updated using the neighboring elements. You need to be careful with formatting here: the asterisk should go directly under the highest digit of the number.

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
For this exam review lab exercise, you will develop a program that fixes faulty data in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //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...

    //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...

    //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...

    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,...

    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> /*...

    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...

    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...

    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...

    #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:" <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT