Question

In: Computer Science

c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...

c++ please

  1. Write and testa C++ main program that:
    1. declare an array arrof 6 integers
    2. Prompt the user for 6 integer values and store them in arr.
    3. Prompt the user for a target integer target.
    4. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed out. The program must use the array traverling pointer notation to traverse the array.

2. Write and test a function search ( ) that searches for an integer s in an array of n elements. If s is found to match an element in the array, the function returns the address of that element; otherwise it returns NULL. The function must use the travelling pointer notation (version-1) to traverse the array. The function has the following prototype.

int* search ( int *p , int s, int n)

Solutions

Expert Solution

#include <iostream>

using namespace std;
int* search ( int *p , int s, int n){
//loop to iterate
for(int i=0;i<n;i++){
//checking if element is exist
//return address
if(*(p+i)==s)
return p+i;
}
//return NULL if the value does not exist
return NULL;
}
int main()
{
//declaring array of 6 elements
int arr[6];
int i,target;
//pointer to store result
int *r;
//reading 6 numbers from user
cout<<"Enter 6 numbers : ";
for(i=0;i<6;i++)
cin>>arr[i];
//reading target element
cout<<"Enter target element: ";
cin>>target;
//calling search to find the target element
r=search(arr,target,6);
//if return value is null than element not found
if(r==NULL){
cout<<"the element target not found";
}
else{
cout<<"Adress of element : "<<r;
}
  
return 0;
}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
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 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()...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
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.
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT