Commit b56c9434 authored by Alejandro E. Rendon's avatar Alejandro E. Rendon
Browse files

Vector Addition

parent c8e94c11
File added
#include <iostream>
#include <string>
using namespace std;
void fillVec(int *vec, int n, int max) {
for (int i = 0; i < n; i++) {
vec[i] = rand() % max;
}
}
void print(string pref, int *vec, int n) {
cout << pref;
for (int i = 0; i < n; i++)
cout << vec[i] << " ";
cout << endl;
}
void add(int *a, int *b, int *c, int n) {
for (int i = 0; i < n; i++)
c[i] = a[i] + b[i];
}
int main(int argc, char const *argv[]) {
if (argc < 2)
cout << "I need more inputs" << endl;
else {
int vec_size = stoi(argv[1]);
int *a, *b, *c;
a = (int *)malloc(vec_size * sizeof(int));
b = (int *)malloc(vec_size * sizeof(int));
c = (int *)malloc(vec_size * sizeof(int));
fillVec(a, vec_size, 10);
fillVec(b, vec_size, 10);
print("a = ", a, vec_size);
print("b = ", b, vec_size);
add(a, b, c, vec_size);
print("c = ", c, vec_size);
free(a);
free(b);
free(c);
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment