Ask a question

Specify And Implement A Method With Header That Returns The Sum Of Element Of A

Posting a C++ solution, that has O(1) access to get the maximum value in the stack.#include#include#include #includeusing namespace std;#define ll long long intint main() {stack s;ll n,t,q;scanf("%lld", &n);for (int i = 0; i < n; i++) {scanf("%lld", &q);//q=1 when push element in stack//q=2 when pop element from stack//q=3 when we have to print maximum element of current stackif(q==1) // push element to stack (always push maximum element at top){scanf("%lld", &t);if (s.empty()) {s.push(t);}else {s.push(max(t, s.top()));}}if(q==2) // pop element from stack{if (!s.empty()) {s.pop();}}if(q==3) //Prints maximum element of stack{printf("%lld\n", s.top());}}return 0;}Basically here we always push the maximum element at top of stack as while pushing element in stack we check if the element to be pushed is greater than top element of stack we push that new element otherwise we push top of stack again thus always maintaining maximum element at the top.Similar Question at Solve Maximum Element

I am going to tell you the easiest way to create your own header files in programming languages like C and C++. For this you do not have to be an expert. This can be done by anyone who has just started learning programming languages.Ok! Before starting the process let me tell you the thing that why we need to create our own header files.==> Why we need to create our own header files?When we are working on big project, we create many functions to perform particular task. But this makes the source code very long. So to solve this kind of problem we create a header file with all those function and include this header file in our program. This makes the program shorter, effective and easy to understand. Now I am sure that you understand the purpose of creating our own header files.==> Simple way to create your own header files in C/C++Watch this video for better understanding- how to create your own header file in turbo c or c++1. Open notepad and write the function that you want to usein your program. An example is shown below.int sum(int a,int b)
{
return(a+b);
}2. Now save the notepad file with .h extension. Like in aboveexample we are creating a function for sum, so save this file with name sum.h inINCLUDE or BIN folder (you can use any other name).3. After that write a program that uses this sum function andinclude the header file that you have just created. I have written the programbelow to make it easy to understand.#include
#include
#include //header file created by you
void main()
{
int a,b,s;
clrscr();
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
s=sum(a,b);
printf(“Sum=%d”,s);
getch();
}
4. In this way you can add more functions to your headerfile.Note: Do not use long header file name (about 7 to 8 characters), otherwise you will get an error. Write only the function definition in the header file, there is no need to write function prototype.

void frequencyCounter(int A[],int n){
int pos = 0;
while(pos < n){
int expectedPos = A[pos] - 1;
if(A[pos] > 0 && A[expectedPos] > 0){
swap(A[pos], A[expectedPos]);
A[expectedPos] = -1;
}
else if(A[pos] > 0){
A[expectedPos] --;
A[pos ++] = 0;
}
else{
pos ++;
}
}
for(int i = 0; i < n; ++i){
printf("%d frequency is %d\n", i + 1 ,abs(A[i]));
}
}
int main(int argc, char* argv[]){
int A[] = {10, 10, 9, 4, 7, 6, 5, 2, 3, 2, 1};
frequencyCounter(A, sizeof(A)/ sizeof(A[0]));
return 0;
}Array should have numbers in the range [1, n].(where n is the size of the array). In frequencyCounter() at any instant,if(A[pos] > 0 && A[expectedPos] > 0)it means that both the numbers at indices pos and expectedPos are actual numbers in the array but not their frequencies.So we will swap them so that the number at the index pos will go to the position where it should have been if the numbers 1, 2, 3, ...., n are kept in 0, 1, 2, ..., n-1 indices. In the above example input array, initially pos=0, so 10 at index 0 will go to index 9 after swap. As this is the first occurrence of 10 make it to -1.(Note that we are storing the frequencies as negative numbers to differentiate between actual numbers and frequencies.)else if(A[pos] > 0)that is A[pos] > 0 && A[expectedPos] < 0it means A[pos] is a number and A[expectedPos] is its frequency without including the occurrence of A[pos]. So increment the frequency by 1 (that is decrement by 1 interms of negative numbers ). As we counted its occurrence we need to move to next pos, so pos++ but before moving to that next position we should make the frequency of the number pos+1 which corresponds to index pos to be zero, since such number is not yet occurred.elseit means current index pos already has the frequency of the number pos+1, so move to next pos, hence pos++.Time Complexity: O(n)Space Complexity: O(1)

Iteration Vs. Recursion Java Coding help?

I won't do your homework for you, but I'll point out that hint (iii) seems to be mistyped. I think you wanted x / 10 and x % 10. When x is an integral typed variable, then x/10 and x%10 give you the quotient and remainder from integer division by 10. When x>=0, then x%10 is equal to the rightmost digit of x. When x>=10 then x/10 is the number you get when the rightmost digit of x is removed.

That should give you an idea how to extract the digits of a number, one at a time, from right to left.

Some extra hints:

Problems 1,2: Since "integer" includes negative numbers, note that the statements I made were only for non-negative x. If you really are supposed to handle negative numbers, then observe that the minus sign is not a digit, so sumOfDigits(x) == sumOfDigits(-x) for all int values of x. Take the absolute value on entry, and you won't have to fix up every remainder or decide whether to compare to 10 or -10.

3. If you want to give your instructor a laugh:

// Multiply: product = m*n
int product = 0;
for (product=0; product
That's sequential addition to form the product. The assignment doesn't say not to use the * operator.

4. Don't attempt the above as an answer to (3) unless you give a good answer for (4). With two inputs you can reduce

5. You seem to have left out some operators in the definition of f(n). You'll need them to write any code. Did you just paste this out of a web assignment without proofing?

6. Uh, really? Stumbling on this one? It's just input, call a function or two and output the results.

Java programming help?!?! sqrt method?

The question is:
Java's Math class contains a static method called sqrt.

Use the sqrt method to create another method called sumSqRoots, which is passed two positive ints as parameters, x and y, and returns the sum of the square root of x and the square root of y. Thus if the method is passed 36 (for x) and 25 (for y), it should return 11.0, because the square root of 36 is 6, and the square root of 25 is 5. Since you will be using the Math.sqrt method, the return type of the method you are to implement is double.

Assume that the Math class is not imported. This means that you must specify the name of the class and use the dot operator when you include the sqrt method in your code. (e.g., Math.sqrt(x))

Here is the code I tried but it's wrong. Can you correct it?
public double sumSqRoots(int x, int y)
{
return (Math.sqrt(x) + Math.sqrt(y));
}
public static void main(String[] args){
SumSqrt sqrt = new SumSqrt();
System.out.println( sqrt.sumSqRoots(36,25) );
}

There’s some theoretical stuff in computer science that involves the study of algebraic monoids. A list or string is what’s known as a “free monoid” over some set of symbols—called the alphabet, it’s basically the type of the elements in a list.There are lots of different concepts you can study with monoids, and it turns out many of them are pretty useful in programs. For example, “mapping” from one list to another—this is creating a new list by applying a function to every element of an old list and preserving the sequence.It also turns out that essentially all of these operations are specific variations on a more general pattern, called a “fold”. The basic idea of a fold is to take a function that takes two arguments, call them type A and type B, and returns a type A, along with an element of type A and a list of type B, and applies the function to each element in the list with the “current” A element to produce a new A element. The fold then returns the final A element.Mapping over a list, summing a list of numbers, computing the length of a list, filtering a list to keep only elements that match some predicate, and many many other operations are examples of folds.The reduce method is an implementation of “fold” for JavaScript arrays being used to represent lists.

Same as all other types. You declare the return type of the function as a vector.i.e.std::vector foo()
{
std::vector example_vector;
//Do something
return example_vector;
}
You can pass a vector by reference too, if you want.

How to solve this with methods in java?

1) Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
2) Write a method that returns the index of the smallest element in an array of doubles. If there
are more than one element that are the “smallest”, then return the smallest index.
3) Write a method that adds two matrices of doubles. For this, your method should check first
whether or not the two matrices are compatible, i.e. they have the same number of elements. If
the matrices are not compatible, print a message to user.
Write a program that tests all these methods as follows. First, the program should ask user to input
two matrices. Then, the program should sum up these matrices. Next, the program should return the
index of the smallest element in the resulted matrix. Finally, the program should output the average of
the resulted matrix.

Yes, there is always a way to turn a recursive algorithm into a non-recursive one. Robert Harper mentioned using the CPS transform, and that will do it.An even more important question is when should you write a recursive algorithm?Recall that the Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, ...where the sequence starts with two 1's and then each element after that is the sum of the previous two elements.For example, here is a simple recursive Java method for computing the Nth Fibonacci number.//assume n >= 1pubic static int fib( int n ){     if ( n == 1 || n == 2 )          return 1;     else          return fib( n-1 ) + fib ( n-2 );}This is a beautifully short method. Sadly, it takes an exponential amount of time to execute. The reason is that every time you call, say. fib(10), it will then call fib(9) and fib(8), and each of these will make more calls until finally your computer turns into a Terminator and wipes you out.So this is a bad way to compute the Fibonacci sequence. Here's a better way://assume n >= 1pubic static int[ ] fib( int n ){     int[ ] fib = new int[n+1]; //create an array to hold the values     fib[ 1 ] = 1;     fib[ 2 ] = 1;          for ( int i = 3; i <= n; i++ )     {              fib[ i ] = fib[ i-1 ] + fib[ i-2 ];     }     return fib;}This is now very efficient and it has the advantage of returning an array of Fibonacci values.So one important thing about recursion is to understand what recursive algorithms are efficient and which ones aren't.