Question

In: Computer Science

Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...

Please Work on the commented parts in the code

#include <stdio.h>
#include <stdlib.h>

/*
*
*/

void printArray(int *arr, int size){
int i;
for( i = 0; i < size; i++) {
// Print each element out
printf("%d ", *(arr+i));
//Print addresses of each element
printf("%p", (arr+i));
//Printing a new line
printf("\n");
}

}

int main() {


// Allows user to specify the original array size, stored in variable n1.
printf("Enter original array size: ");
int n1 = 0;
scanf("%d", &n1);
//a- Create a new array *a1 of n1 ints using malloc
int *a1 = malloc(n1 * sizeof(int));
//b- Remove comments for if statment below
//b- Set each element in a1 to 100 + indexValue ??
// if(a1 == NULL)
// {
// printf("Error! memory not allocated.");
// exit(0);
// }
  
  

printf("Printing the first array allocated using malloc\n");
//c- Print each element and addresses out (to make sure things look right)
  
  
// User specifies the new array size, stored in variable n2.
printf("\nEnter new array size: ");
int n2 = 0;
scanf("%d", &n2);
//d- Dynamically change the array a1 to size n2

//e- Remove comments for if statment below
//e- If the new array is a larger size, set all new members to 200 + indexValue.
// if(a1 == NULL)
// {
// printf("Error! memory not allocated.");
// exit(0);
// }
  
printf("Printing the reallocated array: \n");
//f- Print each element and addresses out (to make sure things look right)


//g-Free the allocated memory for a1


printf("\nEnter new array size to be initialized with 0: ");
int n3 = 0;
scanf("%d", &n3);
//h- Remove comments for if statement
//h- Using calloc create a new array and assign it to a2
//with new array size, stored in variable n3.

// if(a2 == NULL)
// {
// printf("Error! memory not allocated.");
// exit(0);
// }

printf("Printing the array created using calloc: \n");
//i- Print array a2 with size n3


//j- Print array a1 again, How you can fix this problem
printf("Printing deallocated array a1: \n");


// Done with arrays now, free the allocated memory and assign NULL

//free(a2);
//a1 = NULL;
//a2 = NULL;

printf("Program Successfully Completed!");
return 0;


}

Solutions

Expert Solution

In case of any query, do comment. Please rate answer. Thanks

Please use below code:

#include <stdio.h>

#include <stdlib.h>

/*

*

*/

void printArray (int *arr, int size)

{

    int i;

     

    if (arr != NULL)

       

        {

         

        for (i = 0; i < size; i++)

              {

                

                // Print each element out

                                 printf ("%d ", *(arr + i));

                               

                //Print addresses of each element

                                 printf ("%p", (arr + i));

                               

                //Printing a new line

                                 printf ("\n");

             

            }

   

        }

}

int main ()

{

    // Allows user to specify the original array size, stored in variable n1.

    printf ("Enter original array size: ");

    int n1 = 0;

     

    scanf ("%d", &n1);

     

    //a- Create a new array *a1 of n1 ints using malloc

      int *a1 = malloc (n1 * sizeof (int));

     

    //b- Remove comments for if statement below

    //b- Set each element in a1 to 100 + indexValue ??

    if (a1 == NULL)

   

    {

     

        printf ("Error! memory not allocated.");

             

        exit (0);

   

    }

     

    

    for (int index = 0; index < n1; index++)

        {

         

            a1[index] = 100 + index;

       

        }

    

    printf ("Printing the first array allocated using malloc\n");

     

    //c- Print each element and addresses out (to make sure things look right)

    printArray (a1, n1);

     

    

    // User specifies the new array size, stored in variable n2.

    printf ("\nEnter new array size: ");

     

    int n2 = 0;

     

    scanf ("%d", &n2);

     

    //d- Dynamically change the array a1 to size n2

    a1 = malloc (n2 * sizeof (int));

     

    

    //e- Remove comments for if statement below

      

    if (a1 == NULL)

       

    {

         

        printf ("Error! memory not allocated.");

             

        exit (0);

       

    }

     

    

    //e- If the new array is a larger size, set all new members to 200 + indexValue.

    if (n2 > n1)

   

    {

        for (int index = 0; index < n2; index++)

              {

                

            a1[index] = 100 + index;

       

        }

    }

     

    printf ("Printing the reallocated array: \n");

     

    //f- Print each element and addresses out (to make sure things look right)

    printArray (a1, n2);

     

    

    //g-Free the allocated memory for a1

    free (a1);

     

    //added below line to set a1 = NULL

    a1 = NULL;

     

    

    printf ("\nEnter new array size to be initialized with 0: ");

     

    int n3 = 0;

     

    scanf ("%d", &n3);

     

    //h- Remove comments for if statement

    //h- Using calloc create a new array and assign it to a2

    //with new array size, stored in variable n3.

     int *a2 = calloc (n3, sizeof (int));

     

    if (a2 == NULL)

       

        {

         

            printf ("Error! memory not allocated.");

                  

            exit (0);

       

        }

     

    

    printf ("Printing the array created using calloc: \n");

     

    //i- Print array a2 with size n3

    printArray (a2, n3);

     

    

    //j- Print array a1 again, How you can fix this problem

    /* free(a1) does not mean memory can't be accessed. System is free to use the memory. In C, we have to be careful before accessing such pointers

    To fix the above problem, you need to add NULL check into printArray function and after free(a1), set a1=NULL

    */

    printf ("Printing deallocated array a1: \n");

     

    printArray (a1, n2);

     

    

    // Done with arrays now, free the allocated memory and assign NULL

       

    free (a2);

     

    a1 = NULL;

     

    a2 = NULL;

     

    

    printf ("Program Successfully Completed!");

     

    return 0;

}

================Screen shot of the code==========================

Output:


Related Solutions

I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT