
Concept explainers
*C
This exercise is to help you learn how to debug compiler warnings/errors and other common errors in your code. For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. Work on `segfault.c` along with your fixes and error comments.
segfault.c file
// P0
#include <stdio.h>
#include <stdlib.h>
/* Error:
*/
void fib(int* A, int n);
int
main(int argc, char *argv[]) {
int buf[10];
unsigned int i;
char *str;
char *printThisOne;
char *word;
int *integers;
int foo;
int *bar;
char *someText;
// P1
for (i = 0; i <= 10; ++i) {
buf[i] = i;
}
for (i = 0; i <= 10; ++i) {
printf("Index %s = %s\n", i, buf[i]);
}
/* Error:
*/
// P2
str = malloc(sizeof(char) * 10);
strcpy(str, "Something is wrong");
printf("%s\n", printThisOne);
/* Error:
*/
// P3
word = "Part 3";
*(word + 4) = '-';
printf("%s\n", word);
/* Error:
*/
// P4
*(integers + 10) = 10;
printf("Part 4: %d\n", *(integers + 10));
free(integers);
/* Error:
*/
// P5
printf("Print this whole \0 line\n");
/* Error:
*/
// P6
x = 2147483647;
printf("%d is positive\n", x);
x += 1000000000;
printf("%d is positive\n", x);
/* Error:
*/
// P7
printf("Cleaning up memory from previous parts\n");
free(str);
free(buf);
/* Error:
*/
// P8
fib(foo, 7);
printf("fib(7) = %d\n", foo);
/* Error:
*/
// P9
bar = 0;
*bar = 123;
printf("bar = %d\n", *bar);
/* Error:
*/
// P10
someText = malloc(10);
strcpy(someText, "testing");
free(someText);
printf("someText = %s\n", someText);
/* Error:
*/
exit(0);
}
// fib calculates the nth fibonacci number and puts it in A.
// There is nothing wrong with this function.
void fib(int *A, int n)
{
int temp;
if (n == 0 || n == 1)
*A = 1;
else {
fib(A, n - 1);
temp = *A;
fib(A, n - 2);
*A += temp;
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

- >> IN C PROGRAMMING LANGUAGE ONLY << COPY OF DEFAULT CODE, ADD SOLUTION INTO CODE IN C #include <stdio.h>#include <stdlib.h>#include <string.h> #include "GVDie.h" int RollSpecificNumber(GVDie die, int num, int goal) {/* Type your code here. */} int main() {GVDie die = InitGVDie(); // Create a GVDie variabledie = SetSeed(15, die); // Set the GVDie variable with seed value 15int num;int goal;int rolls; scanf("%d", &num);scanf("%d", &goal);rolls = RollSpecificNumber(die, num, goal); // Should return the number of rolls to reach total.printf("It took %d rolls to get a \"%d\" %d times.\n", rolls, num, goal); return 0;}arrow_forwardCalculate average test score: Write a program that reads 10 test scores in an array named testScores. Then pass this array to a function to calculate the average test scores, return the average to main function and display it. Input Validation: The test scores should be between 0 and 100 (inclusive). Add comments and version control.arrow_forwardC++ Get a value from the user and using a loop, search for the value in the array. Report all the index locations where it is found Report if it is not found anywhere This is part 4 of an ongoing assignment RandArray which consisted of assigning 20 integers with a random value. I am not sure how to write this out so it matches the output ( in the attached pictures ). I have attached the starter code to this problem as well as my code for part 3 which was correct. Thank you ( STARTER CODE ) #include <iostream>using namespace std; #include <cstdlib> // required for rand() int main(){// Put Part3 code here //Part 4// ask cout << "Enter value to find: ";// look through the array. // If it is found:// print // Found 55 at index 12 // if it is not found after looking at all the elements, // print // 0 is not found in randArray } _______________________________________________________________________ ( PART 3 CODE ) #include <iostream> using…arrow_forward
- Non dynamic.arrays are allocated at runtime where dynamic arrays are allocated at compile time. True False Question 4 There are other ways to end a loop other than just the loop condition evaluating to false. True Falsearrow_forwardPlease due in C++ If possible can explain what should I put in StudentInfo.h file what in StudentInfo.c Thank youarrow_forwardc++ language using just one for looparrow_forward
- See attached images C++arrow_forwardPlease complete the following guidelines and hints. Using C language. Please use this template: #include <stdio.h>#define MAX 100struct cg { // structure to hold x and y coordinates and massfloat x, y, mass;}masses[MAX];int readin(void){/* Write this function to read in the datainto the array massesnote that this function should return the number ofmasses read in from the file */}void computecg(int n_masses){/* Write this function to compute the C of Gand print the result */}int main(void){int number;if((number = readin()) > 0)computecg(number);return 0;}Testing your workTypical Input from keyboard:40 0 10 1 11 0 11 1 1Typical Output to screen:CoG coordinates are: x = 0.50 y = 0.50arrow_forwardIN C++ Write code that: creates 3 integer values - one can be set to 0, the other two should NOT multiples of one another (for example 3 and 6, or 2 and 8) take you largest value and perform a modulus operation using the smaller (non zero) value as the divisor print out the result. create an alias for the string type call the alias "name" then create an instance of the "name" class and assign it a value using an appropriate cout statement - print out the value Please screenshot your input and output, as the format tends to get messed up. Thank you!arrow_forward
- Please complete this program in C++ Please enusre that there is plenty of comments. PLENTY even if unessessary. Please include screenshots of actual code, screenshots of output, and code in the browser so I can copy and paste. Thank you.arrow_forwardIn C++ please follow the instructions Write two code blocks -- one code block to declare a bag and its companion type-tracking array (both using the STL vector), and another code block to create and declare a Cat object and put it into the bag. Name the arrays and variables as you wish. Use any data type tracking and any designation for cats -- your choices. Assume that struct Cat is already defined and that all required libraries are properly included -- just write the two separate code blocks, separated by one or more blank lines.arrow_forwardDue in C languagearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





