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;...
Part 1: Find and fix errors and add following functionalities 1. There are several syntax errors...
Part 1: Find and fix errors and add following functionalities 1. There are several syntax errors in the code that are preventing this calculator from working, find and fix those errors. When they are fixed, the number buttons will all work, as well as the + (add) and -- (decrement) buttons. a. To find the errors, open up the Developers Tool of the browser and look at the console (F12). Verify the add functionality works. For a binary operator, you...
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
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be...
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] a) if ( sales => 5000 ) puts( "Sales are greater than or equal to $5000" ) else puts( "Sales are less than $5000 ) b) int x = 1, product = 0; while ( x <= 10 ); { product *= x; ++x; } c) While ( x <= 100 ) total =+ x;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT