Part 1:
Determine what is wrong (if any) with the following statements
Part 2:
Create one dimensional array and save the following integers in it: 2, 3, 6, 7, 10, 12. Display the values on the screen. (for example, a[0] = 2, a[1] = 3, etc.)
Part 3:
Write a program to read 5 integers from the user. Save these integers in an array and display the following on screen:
Part 4:
Create a function that will do the following (when called):
Generate 5 random numbers between 1 – 10. (Hint: use rand() function)
Save the random numbers in an array (Hint: create an array)
Display these numbers on the screen by reading them from the saved array.
Part 1:
Part 2:
#include <stdio.h>
int main()
{
int a[6]={2,3,6,7,10,12}; //single dimension array declared and initialized with the given values
printf("The values of array a[] are: ");
for(int i=0;i<6;i++)
{
printf("%d ",a[i]);//printing the array values
}
return 0;
}
Output: The values of array a[] are: 2 3 6 7 10 12
Part 3:
#include <stdio.h>
int main()
{
int a[5],i,sum=0; //single dimension array declared with size 5
double average;
printf("Input the 5 values one by one : \n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]); //Input the array values
}
printf("Entered 5 values are : \n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]); //Input the array values
}
printf("\n"); //new line
for(i=0;i<5;i++)
{
sum=sum+a[i]; //add all the values in array to sum
}
average=sum/5; //calculate average
printf("The average of all the 5 values is: %.2f ",average);
return 0;
}
Output: The ouput for the above code is,
Input the 5 values one by one :
1
2
3
4
5
Entered 5 values are :
1 2 3 4 5
The average of all the 5 values is: 3.00
Part 4:
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
void generate()
{
int a[5],i;
printf("Generating 5 random values between 1 and 10\n");
for(i=0;i<5;i++)
{
a[i]=rand()%10+1; //here we generate random values between 1 and 10 using rand() function and store them in array
}
printf("Values generated and stored in array\n");
printf("Displaying the random generated values: ");
for(i=0;i<5;i++)
{
printf("%d ",a[i]); //display the random values
}
}
int main()
{ srand(time(0)); //this will give the rand() a new seed so that you will get newly generated random values every time. If you dont use this you will get the same values every time you execute the program.
generate(); //call the generate() function
return 0;
}
Output:
Generating 5 random values between 1 and 10
Values generated and stored in array
Displaying the random generated values: 4 7 8 6 4
#Please dont forget to upvote if you find the solution helpful. Thank you.
Get Answers For Free
Most questions answered within 1 hours.