Question

In: Computer Science

Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....

Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist.
Sample input/output #1:
Enter the length of the array: 11
Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43
Output: The array contains 2 of three identical consecutive elements: 2 7
Sample input/output #2:
Enter the length of the array: 4
Enter the elements of the array: -12 0 4 36
Output: The array does NOT contain identical consecutive elements
Sample input/output #3:
Enter the length of the array: 11
Enter the elements of the array: -12 0 4 2 2 2 36 7 7 43 43
Output: The array contains 1 of identical consecutive elements: 2
1) Name your program arrays2.c
2) Include and implement the following function in the program. Do not modify the function prototype.
void search_three(int *a1, int *a2, int n, int *num_three);
The function takes two integer array parameter a1 and a2 of size n. a1 is the input array. a2 is the output array containing the integers that are in the three consecutive integers. num_three as a pointer variable, pointing to the variable for the total number of the three consecutive integers (also the actual size of the output array). For example, the total number of the three consecutive integers for sample input #1 is 2, 0 for sample input #2, and 1 for sample input #3.
The function should use pointer arithmetic – not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
3) In the main function, ask the user to enter the size of the input array, declare the input array and output array of the same size, and then ask the user to enter the elements of the input array. The main function calls search_three function, and displays the result.

Solutions

Expert Solution

C Program

#include <stdio.h>
#include<conio.h>

void search_three(int *a1,int *a2,int n,int *num_three)
{
  
int ans=0;
for(int i=0;i<n-2;i++)
{
if((*(a1+i)==*(a1+i+1)))
{
if(*(a1+i)==*(a1+i+2))
{
*(a2+ans)=*(a1+i);
ans++;
(*num_three)++;
}
}
else
continue;
}
  
if(ans!=0){
printf("The array contains %d of identical consecutive elements: ",*num_three);
//printing the values of final output array.
for(int i=0;i<ans;i++)
printf("%d ",*(a2+i));
return;
}
  
printf("The array does NOT contain identical consecutive elements");
  
}

void main {

int n;
printf("Enter the length of the array: ");
scanf("%d",&n);

printf("\n");
int a1[n];
int a2[n];

printf("Enter the elements of the array: ");
for(int i=0;i<n;i++)
scanf("%d",&a1[i]);
printf("\n");

int count=0;
int *num_three;
num_three=&count;

search_three(a1,a2,n,num_three);
}

EXAMPLES :

INPUT 1:

OUTPUT 2:

INPUT 2 :

OUTPUT 2:


Related Solutions

Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT