Question
c/c++
This is a simple debugging question. The program (as written) tries to add up the elements of two arrays and store the result
5 #include <stdlib.h> #include <stdio.h> UI 3 int main() { int a[5] = {1, 2, 3, 4, 5); int b[5] = {10, 20, 30, 40, 50}; int c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included the entire program below

In your code you need to sum the elements using a for loop:

for(int i=0;i<5;i++)

c[i]=a[i]+b[i]; This will add the elements at the corresponding index.

Moreover array index starts at 0 so for printing the array you must use this loop for(int i=0;i<5;i++)

I have written a generic code which takes input from the user and adds the element.

You code needs the modifications as stated above

#include <iostream>

using namespace std;

int main() {

    int c;

    cout<<"\nEnter the number of columns of the array: "; //Enters the number of columns

    cin>>c;

    int a[c],b[c],sum[c]; //Declaring 3 arrays

    cout<<"\nEnter the elements of array 1: "; //Prompts to enter array 1

    for(int i=0;i<c;i++)

    cin>>a[i];

    cout<<"\nEnter the elements of array 2: "; //Prompts to enter array 2

    for(int i=0;i<c;i++)

    cin>>b[i];

    for(int i=0;i<c;i++)

    sum[i]=a[i]+b[i]; //Computes the sum of array

    cout<<"\nResultant array is: ";

    for(int i=0;i<c;i++) //Displays resultant array

    cout<<sum[i]<<" ";

}


1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std; int main() { int c; cout<<\nEnter the number of columns of the ar

Add a comment
Know the answer?
Add Answer to:
c/c++ This is a simple debugging question. The program (as written) tries to add up the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c/c++ Last name: First name: ID: Sec: The name, ID and section number that appear at...

    c/c++ Last name: First name: ID: Sec: The name, ID and section number that appear at the beginning of the output of the program should be your own, not the ones used in the sample Note: results printed must show the data entered at run time, not the data shown in the sample run below! Sample run: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24...

  • c/c++ Passing arrays as arguments to functions. The main() is given, write the function 'average' (Study...

    c/c++ Passing arrays as arguments to functions. The main() is given, write the function 'average' (Study the syntax of passing an array as a function parameter) The program prompts the user to first enter the number of tests given in a course, and then prompts him/her to enter the mark for each test. The marks are stored in an array. This takes place in the main() function. It then passes the array to a function named 'average' that calculates and...

  • A programmer is having difficulty debugging the following C program. In theory, with infinite memory, this...

    A programmer is having difficulty debugging the following C program. In theory, with infinite memory, this program would run forever but in practice, this program crashes because it runs out of memory. Explain the behavior of the program by translating the definition of f into lambda calculus and then reducing the application f(f). This program assumes that the type checker does not check the types of arguments to functions. int f(int (*g)(...)) { /* g points to a function that...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • A)Correct this code and explain what was corrected // C code - For Loop / Array...

    A)Correct this code and explain what was corrected // C code - For Loop / Array Population // This program will populate integers into an array, and then display those integers. // Developer: Johnson, William CMIS102 // Date: Mar 4, 2020 // Global constants #define INTLIMIT 10 #include <stdio.h> // This function will handle printing my name, class/section number, and the date void printStudentData() { char fullName[19] = "William J. Johnson"; char classNumber[9] = "CMIS 102"; char sectionNumber[5] = "4020";...

  • Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in...

    Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array elements are placed in a list enclosed in curly braces after the array name definition. For example, the code below creates an array of ints with 3 elements: 1, 2 and 3. int[] a = {1, 2, 3, 4}; We can also initialize an array with a new construct, indicating how many elements we want...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • C++ ArrayMethods Program

    Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an arrayint arrayMin(int[] arr) returns the minimum value in an arrayvoid arraySquared(int[] arr) changes every value in the array to its square (value²)void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9  becomes 9   8   7 ) The program main method creates a single-dimensional array of length 5 elements and initialize it with random...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT