1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tamaranim1 [39]
3 years ago
13

Write a generic C++ function that takes an array of genericelements and a scalar of the same type as the array elements. Thetype

of the array elements and the scalar is the generic parameter.The function must search the given array for the given scalar andreturn the subscript of the scalar in the array. If the scalar isnot in the array, the function must return -1. Test the function for int and float types.
Requirements:
1. Write a main function to make it acomplete program.
2. Initialize the array with random numbers.
Computers and Technology
1 answer:
siniylev [52]3 years ago
7 0

Answer:

Explanation:

Here is the code

#include<iostream>

using namespace std;

template <class myType>

int generic(myType *a, myType b)

{

int i;

for(i=0;a[i]!=-1;i++)

{

       if( b == a[i])

       return i;

}

return -1;

}

int main()

{

int a[]={1,2,3,4,5,6,7,8,-1};

float b[]={1.1,2.1,3.1,4.1 ,5.1,6.1,7.1,-1};

if(generic(a,5)>0)

       {

       cout<<" 5 is foundin int at index "<<generic(a,5)<<endl;

       }

       else

       {

       cout<<" 5 notfound "<<endl;

       }

if(generic(b,(float)5.1)>0)

       {

       cout<<" 5.1 isfound in float at index "<<generic(a,5)<<endl;

       }

       else

       {

       cout<<" 5.1 notfound "<<endl;

       }

system("pause");

}

/*

sample output

5 is found in int at index 4

5.1 is found in float at index 4

*/

You might be interested in
Discuss two things you’ve learned about computers
olga55 [171]

Answer:

Time management,

Refreshment,proper input, Need of energy,prioritization.

7 0
3 years ago
Write a program binary.cpp that reads a positive integer and prints all of its binary digits.
earnstyle [38]

Answer:

View image below.

Explanation:

They're just asking you to create a num2bin function on Cpp.

If you search for "num2bin in C", you can find a bunch of answers for this if you think my solution is too confusing.

One way to do it is the way I did it in the picture.

Just copy that function and use it in your program. The rest you can do yourself.

5 0
3 years ago
Define the term E-learning​
MaRussiya [10]
Elerning means learning on the internet
3 0
3 years ago
Is this correct just need somebody to check my answer plz and use your honest answer
Mademuasel [1]

Answer:

your answer looks correct

6 0
3 years ago
Each of k jars contains m white and n black balls. A ball is randomly chosen from jar 1 and transferred to jar 2, then a ball is
Elodia [21]

Answer:

#initial probability of getting a white ball

init_prob_white=15/25

#initial probability of getting a black ball

init_prob_black=10/25

#probability of getting a white ball given

#that we transferred a white ball in previous

#step is 16/26 which we represent by pww

pww=16/26

#probability of getting a white ball given

#that we transferred a black ball in previous

#step is 15/26 which we represent by pwb

pwb=15/26

#similarly

pbb=11/26;

pbw=10/26;

#The below two functions are recursive which calculate

#total probability right from the start

#The formula they use are

# Prob(W_k)=Prob(W_k-1)*Prob(W_k | W_k-1)+

# Prob(B_k-1)*Prob(W_k | B_k-1)

def pw(n):

if n==1:

return init_prob_white;

else:

return (((pww)*pw(n-1))+((pwb)*pb(n-1)))

def pb(n):

if n==1:

return 10/25;

else:

return (((pbb)*pb(n-1))+ ((pbw)*pw(n-1)))

#for k=5

k=5

x=pw(k)

print("After "+str(k)+" transfers probability="+str(x))

Explanation:

3 0
3 years ago
Other questions:
  • When you first turn on a computer and you don’t hear a spinning drive or fan or see indicator lights, is the problem hardware or
    13·1 answer
  • ____ takes the idea of breaking down a program into separate and reusable functions to the next level by focusing on the encapsu
    10·1 answer
  • Which of these is a consistent periodic pulse separated into equivalent intervals of time?
    14·2 answers
  • What are indexes in a database?
    14·1 answer
  • Write calls to printf to display the variable x in the following formats:
    8·1 answer
  • Can You Get Commission Shortcut For FREE?
    15·1 answer
  • What will be displayed if the following Java code segment is run? System.out.println("one "); System.out.print("two "); System.o
    12·1 answer
  • A command, such as a button or keyboard shortcut, that performs a specific task is known as a
    9·2 answers
  • One advantage of a PAN​
    10·2 answers
  • Information that's been collected to help solve a problem is called
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!