Write and run a C program that adds the elements of two 5-element arrays A and B
ID: 3872455 • Letter: W
Question
Write and run a C program that adds the elements of two 5-element arrays A and B, depositing the results in a third array C. That is, for every i ranging from 0 to 4, C[i] = A[i] + B[i]. Prompt the user to enter 10 numbers, with which you will populate the arrays A and B. Print out array C after it has been computed with a loop. Try using a "while" loop, then a "for" loop, and finally a loop using "goto." Write a program to do the same thing using MIPS assembly language. If you use the SPIM simulator, note that sometimes it doesn't like single-character array names, and so use some other names. Problem 2.10. However, you must run both the MIPS program and the C program. Of course this means that each program must have all the components necessary for building and running. Submit the source code files and also a recording of the run in some form, such as a screenshot. Problem 2.12. Actually write runnable code, run it and report on the real results and reasons for those results. Problem 2.20. Again actually run your code, submit and analyze the results to determine whether or not they are as expected.
Explanation / Answer
ANSWER::
#include<stdio.h>
#define MAX 20
void readArray(int a[],int size)
{
int i;
for(i=0;i< size;i++)
{
printf("Enter %d element :",i+1);
scanf("%d",&a[i]);
}
}
void printArray(int a[],int size)
{
int i;
for(i=0;i < size; i++)
printf("%5d",a[i]);
}
void addArray(int a[],int b[],int c[],int size)
{
int i;
for(i=0; i< size;i++)
c[i]=a[i]+b[i];
}
int main()
{
int A[MAX],B[MAX],ADD[MAX];
int i,n;
printf(" Enter size of an Array :");
scanf("%d",&n);
printf(" Enter elements of Array 1: ");
readArray(A,n);
printf(" Enter elements of Array 2: ");
readArray(B,n);
/* add Arrays*/
addArray(A,B,ADD,n);
printf(" Array elements after adding : ");
printArray(ADD,n);
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.