Answer to Question #191179 in C++ for python

Question #191179

1. Write a C++ program that will add two single dimensional arrays elements using random numbers?


1
Expert's answer
2021-05-16T23:47:33-0400
#include <iostream>
#include <time.h>
using namespace std;
int main(){
    srand(time(NULL));
    int length = rand() % 10;
    int *arr1 = new int[length];
    int *arr2 = new int[length];
    int *arr3 = new int[length];
    for(int i = 0; i < length; i++){
        arr1[i] = rand() % 100;
        arr2[i] = rand() % 100;
    }
    cout<<"Array 1\n";
    for(int i = 0; i < length; i++) cout<<arr1[i]<<" ";
    cout<<"\nArray 2\n";
    for(int i = 0; i < length; i++) cout<<arr2[i]<<" ";
    cout<<"\nArray 1 + Array 2\n";
    for(int i = 0; i < length; i++) arr3[i] = arr1[i] + arr2[i];
    for(int i = 0; i < length; i++) cout<<arr3[i]<<" ";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment