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...

  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

  • 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...

  • Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

  • Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding...

    Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding array elements, that array1 [0] + array2[0], etc. Save the sum into a third array called sumArray[]. Display the sum array. Submit your and the input and output of the complete execution.

  • Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...

    Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at even subscripts 2. The sum of all elements at odd subscripts 3. The sum of all elements You are allowed to perform this functionality within main. Main program: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include <stdio.h> int main() { /* Declare array variables */ const int NUM_VALS = 4; int userValues[NUM_VALS]; int i; // counter int even = 0; int odd = 0; int sum = 0; /* Initialize...

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