Lab8.c given below. Have to make sure it has the following 6 files. Im so confus
ID: 3811109 • Letter: L
Question
Lab8.c given below. Have to make sure it has the following 6 files. Im so confused on this. Need super help.
/*
Wesley Grant
Lab:01
3/17/2017
Lab 8
user choose 4 different ways of initializing each array. After each initialization,the array should be printed to the user to show it was initialized correctly by calling a printArray funtion.
*/
#include <stdio.h>
void numbers(int array[20])
{
int i;
for(i=0;i<20;i++)
{
array[i]=i+1;
}
}
void evenNumbers(int array[20])
{
int i;
int j=2;
for(i=0;i<20;i++)
{
array[i]=j;
j +=2;
}
}
void oddNumbers(int array[20])
{
int i;
int j=1;
for(i=0;i<20;i++)
{
array[i]=j;
j+=2;
}
}
void fibbonacci(int array[20])
{
int i;
int n1=0;
int n2=1;
array[0]=n1;
array[1]=n2;
for(i=2;i<20;i++)
{
array[i]=n1+n2;
n1=n2;
n2=array[i];
}
}
int main(void)
{
int n=0;
int array1[20],array2[20],array;
int product=0;
int i;
int option=0;
do
{
printf("Choose 1-4 to intialize an array");
printf(" 1.all numbers from 1-20");
printf(" 2.the first 20 even numbers beginning with 2");
printf(" 3.the 20 odd numbers beginning with 1");
printf(" 4.the first 20 fibbonacci numbers, beginning with 0 and 1 ");
scanf("%d",&option);
switch(option)
{
case 1: if(n==0)
numbers(array1);
else
numbers(array2);
break;
case 2: if(n==0)
evenNumbers(array1);
else
evenNumbers(array2);
break;
case 3: if(n==0)
oddNumbers(array1);
else
oddNumbers(array2);
break;
case 4: if(n==0)
fibbonacci(array1);
else
fibbonacci(array2);
break;
}
n++;
if(n==2)
break;
}while(n<=2);
for(i=0;i<20;i++)
{
printf("%d ",array1[i]);
}
printf(" ");
for(i=0;i<20;i++)
{
printf("%d ",array2[i]);
}
printf(" ");
for(i=0;i<20;i++)
{
product = product + array1[i]*array2[i];
}
printf("The inner product of the arrays are %d ",product);
return 0;
}
Explanation / Answer
//Header file
//Maximum size is 100
#define MAX 100
//prototype declaration of functions
void numbers(int array[20]);
void evenNumbers(int array[20]);
void oddNumbers(int array[20]);
void fibbonacci(int array[20]);
//Initialize.c file
//Function to initialize an array
void numbers(int array[20])
{
int i;
//Loops till 20
for(i=0;i<20;i++)
{
array[i]=i+1;
}//End of loop
}//End of function
//To generate and store even numbers in array
void evenNumbers(int array[20])
{
int i;
int j=2;
for(i=0;i<20;i++)
{
array[i] = j;
//Adds 2 to previous value of j
j += 2;
}//End of loop
}//End of function
//Function to generate and store odd numbers in array
void oddNumbers(int array[20])
{
int i;
int j=1;
for(i=0;i<20;i++)
{
array[i]=j;
//Adds 2 to previous value of j
j+=2;
}//End of loop
}//End of function
//To generate fibbonacci series and store it in array
void fibbonacci(int array[20])
{
int i;
int n1=0;
int n2=1;
array[0]=n1;
array[1]=n2;
for(i=2;i<20;i++)
{
array[i]=n1+n2;
n1=n2;
n2=array[i];
}//End of loop
}//End of function
//print.c file
#include<stdio.h>
//Displays array contents
void print(int arr[20])
{
int x;
for(x = 0; x < 20; x++)
printf("%6d", arr[x]);
}//End of function
//lab8.c file
#include<stdio.h>
#include"lab10.h"
#include "print.c"
#include"initialize.c"
//Main function
int main(void)
{
int n = 0;
int array1[20],array2[20],array;
int product = 0;
int i;
int option = 0;
//Displays menu
do
{
printf(" Choose 1-4 to intialize an array");
printf(" 1.all numbers from 1-20");
printf(" 2.the first 20 even numbers beginning with 2");
printf(" 3.the 20 odd numbers beginning with 1");
printf(" 4.the first 20 fibbonacci numbers, beginning with 0 and 1 ");
//Accepts user choice
scanf("%d", &option);
switch(option)
{
case 1:
if(n == 0)
numbers(array1);
else
numbers(array2);
break;
case 2:
if(n==0)
evenNumbers(array1);
else
evenNumbers(array2);
break;
case 3:
if(n==0)
oddNumbers(array1);
else
oddNumbers(array2);
break;
case 4:
if(n==0)
fibbonacci(array1);
else
fibbonacci(array2);
break;
}//End of switch
n++;
if(n == 2)
break;
}while(n<=2);
print(array1);
printf(" ");
print(array2);
printf(" ");
for(i=0;i<20;i++)
{
product = product + array1[i]*array2[i];
}
printf("The inner product of the arrays are %d ",product);
return 0;
}//End of main
Sample run1:
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
1
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
The inner product of the arrays are 5740
Sample run2:
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
1
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
The inner product of the arrays are 5530
Sample run3:
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
1
Choose 1-4 to intialize an array
1.all numbers from 1-20
2.the first 20 even numbers beginning with 2
3.the 20 odd numbers beginning with 1
4.the first 20 fibbonacci numbers, beginning with 0 and 1
4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
The inner product of the arrays are 201210
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.