Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void printArray(int * a, int size) {
- for (int i=0; i<size; i++) {
- printf("%d", a[i]);
- if (i==size-1) putchar('\n');
- else printf(", ");
- }
- }
- int product (const int * v1, const int * v2, unsigned int size) {
- int result=0;
- for (unsigned i=0; i<size; i++) {
- result = result + v1[i]*v2[i];
- }
- return result;
- }
- int main(int argc, char * argv[])
- {
- srand(time(NULL));
- int size=2;
- if (argc>1) {
- int a=atoi(argv[1]);
- if (a>0) size=a;
- }
- int * array1, * array2;
- int status;
- do {
- array1=malloc(size * sizeof(int));
- array2=malloc(size * sizeof(int));
- if (array1 == NULL || array2==NULL) {
- fputs("Memory allocation error!", stderr);
- return EXIT_FAILURE;
- }
- for (int i=0; i<size; i++) {
- array1[i] = rand() % 200 - 100;
- array2[i] = rand() % 200 - 100;
- }
- //printArray(array1, size);
- //printArray(array2, size);
- printf("Scalar product= %d\n", product(array1, array2, size));
- puts("Let's try again - enter a new size!");
- status = scanf("%d", &size);
- free(array1);
- free(array2);
- } while (status>0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement