Question

For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...

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. And fix the code to make it work.

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

Homework Answers

Answer #1

i have written the reason after every error between /*Error */ don't forget to declare temp variables in starting of main function

// P0   
#include <stdio.h>

#include<string.h>
#include <stdlib.h>
/* Error:
   use header file string.h
*/

void fib(int* A, int n);

int main(int argc, char *argv[]) {
   int buf[10];
   unsigned int i,x;
   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 %d = %d\n", i, buf[i]);
   }
   /* Error:
       i and buf are of integer type use %d insted of %s
   since %s is for string.
   */

   // P2
   str =(char *) malloc(10 * sizeof(char) );
   strcpy(str, "Something is wrong");
   printf("%s\n", printThisOne);
   /* Error:
   ->you have to use explicit type conversion to convert in char type
   ->to use strcmp you have to use string.h headerfile
   */

   // P3
   word = "Part 3";
   printf("%s\n", word);
   word+=4;
   word="-";
   printf("%s\n", word);

   /* Error:
   you have to give proper address in left hand side for assinging a value.
    since (word+4) is not giving a proper address on left hand of '=' so it will give    error of
   'lvalue required as left operand of assignment'
   or you can also use
   so firt you have increment the value and the assing it
   */

   // P4

   int temp=10;
        integers+=10;
       integers = &temp;
       printf("Part 4: %d\n", *integers );
       integers=NULL;
   /* Error:
   initialize temp in starting
   error is same as in 3
   and clear the memory for pointer you have assign it to NULL
   free function is used when memory is assigned using malloc function

   */

   // P5
   printf("Print this whole \0 line\n");
   /* Error:
       NO ERROR
   */

   // P6
   x = 2147483647;
   printf("%d is positive\n", x);
   x += 1000000000;
   printf("%d is positive\n", x);
   /* Error:
       x was defined above
   */

   // P7
   printf("Cleaning up memory from previous parts\n");
   free(str);
   memset(buf, 0, sizeof(buf));
   /* Error:
   free function used when memeroy is allocated using malloc
   to clean buf you have to use memset
   */

   // P8 solve&
   fib(&foo, 7);
   printf("fib(7) = %d\n", foo);
   /* Error:
       use '&' operator to pass the address
   */

   // P9
   int temp=123
   bar = &temp;
   bar = &temp;
   printf("bar = %d\n", *bar);
   /* Error:
   you're not initializing the pointer. You've created a pointer to "anywhere you want"—which could be the address of some other variable, or the middle of your code, or some memory that isn't mapped at all.
so you have to use an interger variable to assing it to a pointer
   intialize the temp variable in starting
   */

   // P10 solve (char *)
   someText =(char *) malloc(10);
   strcpy(someText, "testing");
   free(someText);
   printf("someText = %s\n", someText);
   /* Error:
   ->you have to use explicit type conversion to convert in char type
   ->to use strcmp you have to use string.h headerfile
   */
   */

   exit(0);
   return 0;
   /* use of return 0 in mandatory */
}


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

Know the answer?
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
Similar Questions
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25 points) // Note: You may notice some warnings for variables when you compile in GCC, that is okay. #define macro_1(x) ((x > 0) ? x : 0) #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10) int function_1(int a, int b) { return (3*a - 3*b*b + 4*a * b - a*b * 10); } // Part 1: //...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT