Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve code using the C langauge! Do not recycle old answers. Please add to the sample code provided. Squares sample code: #include /* This

Please solve code using the C langauge! Do not recycle old answers. Please add to the sample code provided.

Squares sample code:

#include  /* This program should print the sum of the elements * 1^2, 2^2, 3^2, ..., n^2 * where n is an integer provided by the user on the * command line. Hunt down the bugs and squash them! * Seek out the memory leaks and plug them up! */ /* Computes the sum of the first n elements in the array. */ int sum(int n, int* arr) { int i, sum; for(i = 0; i  

argvcat sample code:

#include  #include  #include  /* print out an error message and exit */ void my_error(char *s) { perror(s); exit(1); } /* Concatnate two strings. * Dynamically allocate space for the result. * Return the address of the result. */ char *my_strcat(char *s1, char *s2) { // TODO return NULL; } int main(int argc, char *argv[]) { char *s; s = my_strcat("", argv[0]); for (int i = 1; i  

image text in transcribed

We are going to learn another useful tool valgrind, which helps you to find bugs in programs. The official site of valgrind has a quick start guide. Part 1. squares. Use gdb and valgrind to find and correct all compiler warnings and bugs in squares. c. You may need to look at the man pages for any library functions used in the code (e.g., atoi ()) to understand their function and to ensure they are being used correctly. This exercise provides us an opportunity to debug other people's code. Completely rewriting the program will not let us learn as much. Part 2. argvcat. argvcat is a program that concatenates all the arguments in argv to a single string and prints out the concatenated string. Starter code is provided in argvcat. c. The goals of this assignment is to 1) complete the function my_strcat() and 2) fix all memory leaks in the code (Of course, you just learned valgrind.) The function my_strcat() has the following prototype: char my_strcat (chars1, char s2); The function takes two strings, s1 and s2, as parameters and returns a new string that is the concatenation of s1 and s2. The memory space for storing the return string should be dynamically allocated from the heap. The function should request the least amount of space that is needed to hold the result. The function should not change the strings s1 or s2. For example, the function should not try to free the memory used by s1 or s2 because 1) their memory space may not be dynamically allocated, and 2) the caller may want to keep s1 or s2. You can use string functions in C library, for example, strlen() and strcat(). Read man pages to learn them. If malloc () fails, call my_error () to print an error message and exit. It is necessary to add code in the main() function to free memory. However, the changes should not affect how my_strcat() is called and how the loop is constructed. The program should work as in the sample runs listed below after my_strcat() is implemented, even before memory leaks are fixed. The final program should not have memory leaks. \$./argvcat ./argvcat \$./argvcat a 1 b 2234 ./argvcata1b2234 \$./argvcat 1 a '' '\#.' and more arguments

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

1. Practice giving the test before you actually use it.

Answered: 1 week ago