Question

In: Computer Science

The following code segment which uses pointers may contain logic and syntax errors. Find and correct...

The following code segment which uses pointers may contain logic and syntax errors. Find and correct them.

a) Returns the sum of all the elements in summands

int sum(int* values)
{
int sum = 0;
for (int i = 0; i < sizeof(values); i++)
sum += *(values + i);
return sum;
}

b) Overwrites an input string src with "61C is awesome!" if there's room. Does nothing if there is not. Assume that length correctly represents the length of src.

void cs61c(char* src, size_t length) {
char *srcptr, replaceptr;
char replacement[16] = "61C is awesome!";
srcptr = src;
replaceptr = replacement;
if (length >= 16) {
  
for (int i = 0; i < 16; i++)
*srcptr++ = *replaceptr++;
}
}

Solutions

Expert Solution

1)

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <stdio.h>

// We can't find the size of the array from a function.
// We have to pass the size as a parameter to the function.
int sum(int* values,int size)
{
int sum = 0;
//use the size variable here
for (int i = 0; i < size; i++)
sum += *(values + i);
return sum;
}

//testing the sum() function
int main() {
int values[5]={1,2,3,4,5};
int size = sizeof(values) / sizeof(values[0]);
   printf("%d",sum(values,size));
   return 0;
}

====================

2)

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <stdio.h>
void cs61c(char* src, size_t length)
{
//change *replaceptr here
char *srcptr, *replaceptr;
char replacement[16] = "61C is awesome!";
srcptr = src;
replaceptr = replacement;
if (length >= 16)
{
for (int i = 0; i < 16; i++)
{
*srcptr++ = *(replaceptr)++;
}
  
}
}

//testing the function.
int main() {
char src[] = "PraveenKumarREDDYkumar";
   cs61c(src,24);
   printf("%s",src);
   return 0;
}

-================


Related Solutions

The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Please Fix code and make Copy/Paste avaliable // Application allows user to enter a series of words // and displays them in reverse order import java.util.*; public class DebugEight4 {    public static void main(String[] args)    {       Scanner input = new Scanner(System.in);       int...
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
To earn full credit, program must be free of syntax, run-time, and logic errors; include program...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program comments; use reasonable readable variable names and prompts. To include variables in the input prompt, you must use concatenation character (+). For example: assume you already asked for input of employee's first name and last name (they are stored into variables FirstName and LastName, then use this prompt to ask for employee's weekly hours which includes the employee's full name in the input statement....
Does the data contain errors? If so, write queries in SQL to find these errors and...
Does the data contain errors? If so, write queries in SQL to find these errors and propose a way to address the issues Theres a change of weight error (end weight-start weight) calculated wrong and a logical error
1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
Please edit for clarity and conciseness The following document may contain errors in grammar, capitalization, punctuation,...
Please edit for clarity and conciseness The following document may contain errors in grammar, capitalization, punctuation, abbreviation, number style, word division, and vocabulary. Memco Construction 187 W. Euclid Avenue, Glenview, ILL 60025 www.memco.com April 19, 2015 PROJECT: IDOT Letting Item #83 Contract No. 79371 DuPage County Dear Mr. Estes— Memco Construction is pleased to submit a road construction proposal for the above project. Our company has been providing quality materials and subcontracting services for highway reconstruction projects for over twenty-three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT