Identify an error in the following C statements: Will the error be detected? If
ID: 3559424 • Letter: I
Question
Identify an error in the following C statements: Will the error be detected? If so, when? Declare an array of type double values called exper that can be referenced by using any day of the week as a subscript, where 0 represents Sunday, 1 represents Monday, and so on. The statement marked /* this one */ in the following code is valid. True or false? What are the two common ways of selecting array elements for processing? Write a C program segment to display the index of the smallest and the largest numbers in an array x of 20 integers. Assume array x already has values assigned to each element. Write a C function called reverse that takes an array named x as an input parameter and an array named y as an output parameter. A third function parameter is n, the number of values in x. The function should copy the integers in x into y but in reverse order (i.e., y[0] gets x[n - 1],...,y[n - 1] gets x[0]).Explanation / Answer
1. yes there is an error we have decalred an araay of size 8 in which we can access elements indexed from 0 to index 7 but in our loop we have tried to access the index 8 which is illegal but this eroor will get unnoticed by compiler because complier performs no bounds checking on our arrays, this error can be identifiied only when array doesn't have space to allocate 8 values and then we try to put some value in that invalid address location
2. first we have to define an enumeration
enum days
{
sunday,
monday,
tuesday,
wednesday,
thursday.
friday,
saturday
};
now defoine the array
double exper[days];
3.yes. statement is valid because counts[i] will give us a value between 0 and 4 which can be taken as index for array x
4.first way is arr[i] where i is index and arr is name of array
another is *(arr + i)
5.
#include<stdio.h>
#include<conio.h>
int main(){
int a[50],size,i,big,small;
printf(" Enter the size of the array: ");
scanf("%d",&size);
printf(" Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=0;
for(i=1;i<size;i++){
if(big<a[i])
big=i;
}
printf("Largest element index: %d",big);
small=0;
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element index: %d",small);
getch();
return 0;
}
6.
#include<stdio.h>
#include<conio.h>
#define SIZE 100
void function(int *arr1, int *arr2, int sz)
{
int i;
for(i=0;i<sz;i++)
arr2[sz-1-i]=arr1[i];
}
int main()
{
int arr1[SIZE], arr2[SIZE],sz,i;
printf("Enter size ");
scanf("%d ",&sz);
for(i=0;i<sz;i++)
scanf("%d",arr1+i);
function(arr1,arr2,sz);
printf("Array 1 is: ");
for(i=0;i<sz;i++)
printf("%d ",arr1[i]);
printf("Array 2 is: ");
for(i=0;i<sz;i++)
printf("%d ",arr2[i]);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.