/*
Haylee Smith
2/17/2023
CPS 1010 Lab Section 004, Spring 2023
Lab #4
In this program, two arrays are created and sorted. 
The second largest number of each array is found.
The inner products of array 1 and array 2 is calculated.
*/

#include <stdio.h>
#include <stdlib.h>

int main(){
	int array1[20] , array2[20], i;
	int j, temp, max = 1, secondmax = 1;
	int max2 = 1, nlmax = 1;
	int product_array[20], inner_product = 0;

	//Array 1
	printf("Enter 20 integers:\n");
	for (i = 0; i < 20; i++){
		fscanf(stdin, "%d", &array1[i]);
	}
	printf("\nArray1:\n");
	for (i = 0; i < 20; i++){
		printf("%d\n", array1[i]);
	}
	
	//Array 2
	for (i = 0; i < 20; i++){
		array2[i]= rand() % 50 + 1;
	}
	printf("\nArray2:\n");
	for (i = 0; i< 20; i++){
		printf("%d\n", array2[i]);
	}
	
	//Sorting Array 1
   for (i = 0; i <= 19; ++i ){
      for (j = i + 1; j < 20; ++j){
         if (array1[i] > array1[j]){
            temp = array1[i];
            array1[i] = array1[j];
            array1[j] = temp;
         }
      }
   }

	//Sorting Array 2
   for (i = 0; i <= 19; ++i ){
      for (j = i + 1; j < 20; ++j){
         if (array2[i] > array2[j]){
            temp = array2[i];
            array2[i] = array2[j];
            array2[j] = temp;
         }
      }
   }

	//Max number in Array 1
	for (i = 0; i < 20; ++i ){
		if(max < array1[i]){
			max = array1[i];
		}
	}

	//Max number in Array 2
   for (i = 0; i < 20; ++i ){
      if(max2 < array2[i]){
         max2 = array2[i];
      }
   }

	// Second largest in array 1 and 2
	for (i = 0; i < 19; ++i ){
		if (secondmax < array1[i]){
			if (secondmax != max){
				secondmax = array1[i];
			}
		}
		if (nlmax < array2[i]){
         if (nlmax != max2){
            nlmax = array2[i];
         }
      }
   }
	printf("\nThe second largest number of array 1 is %d\n", secondmax);
   printf("The second largest number of array 2 is %d\n", nlmax);

	// inner product
	for (i = 0; i < 19; ++i ){
		product_array[i] = array1[i] * array2[i];
	}
	for (i = 0; i < 19; ++i ){
		inner_product += product_array[i];
	}
	printf("\nThe inner product of array 1 and 2 is: %d\n\n", inner_product);

	return 0;
}
