Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,604 members, 7,809,198 topics. Date: Friday, 26 April 2024 at 04:33 AM

Help Needed With The Following Questions In C++ Or Any Language Please!!! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Needed With The Following Questions In C++ Or Any Language Please!!! (1647 Views)

Whatsapp Group Chat For C# &/or C++ / Creating UWP Application In C# Or C++ (xaml) / C Or C++, Which Is Best For Arduino Programming? (2) (3) (4)

(1) (Reply) (Go Down)

Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 4:21am On Jan 15, 2016
Please help a beginner

Question 1. Write a function that takes three integers as arguments and returns the product of
the largest two.

#include<iostream>
int maxmul(int a, int b, int c)
{
}
int main()
{
cout << maxmul(1,2,3) << endl;
cout << maxmul(3,1,2) << endl; cout << maxmul(3,2,1) << endl;
}

Question 2. Write a function, inc, that increases the value of variable x by 1.0;

#include<iostream>
int main()
{
float x=10.0;
cout << inc(x) << endl;
}

Question 3. Write a function that calculates and returns the average value of an array of
doubles. The length of the array is provided as a second argument, n.

double average_array(double data[], int n)
{
}

Question 4. Write a function to subtract the average value of the entire array from each array
element. You may make use of the average_array function from the first question.

void subtract_average(double data[], int n)
{
}

Question 5. Write a function to calculate the average value of a 100 by 100 array of ints.
Return the average as a double.

question 6. Write a program to dynamically allocate a n by m array of doubles. Both n and m
should be read from the console. All elements of the array should be initialized to zero. All
memory should be freed before the program exits.


Question 7. The function trap() numerically integrates exp(x^2) in the interval x=[a,b]. n is the number of points to evaluate the function at. Add comments to the function and format it using C++ standards.

#include<cmath>
float exp_mx2(int x)
{return(exp(x*x));}
float trap(float a, float b, int n) { float h = (ba)/(n1); float value = 0.5*( f(a)+
f(b)); for(int k=2; k < n; k++){value+=f( a + h*(k1)); } value*=h;return(value); }
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Otuabaroku: 6:39am On Jan 15, 2016

@Op, I think this is an assignment given to you to do. It makes sense for you to make an attempt. When people see your effort, then you can be helped. In that way,you learn more from the assignment.

3 Likes

Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 6:59pm On Jan 15, 2016
solution 1

#include <iostream>
using namespace std;
int main ( )
{
int maximum (int a, inb,intc)
{
int a =1;
int b=2;
int b=3;
product =inta*intb
cout <<product<< endl;
return 0
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 7:05pm On Jan 15, 2016
solution2

#include <iostream>
using namespace std;
int main ( )
{
int x=10 ;
++x;
for (inti=0;i <10;++i);
cout <<++x<<endl;
return 0;
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 7:13pm On Jan 15, 2016
solution 3
#include <iostream>
Using namespace std;
int main ( )
{
double data [] = {a,b} ;
cout <<"Enter two numbers :add\n";
cin>>a>>b;
average =(a+b)*2
cout <<average<< endl;
return 0 ;
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 7:21pm On Jan 15, 2016
solution 5

#include <iostream>

Using namespace std;
int main ( )
{
int array [] ={100,100} ;
// calculate the Average
int sum =0 ;
for (inti=0;i <numbers.length; i++) ;
double average =sum /numbers.length;
cout <<average <<endl ;
return 0 ;
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 7:27pm On Jan 15, 2016
solution 6


#include <iostream>
Using namespace std;
int main ( )
{
array double [] = {m,n} =0
cin>>m>>n;
std:cout <<array double << std ::endl;
delete [] array ;
return 0 ;
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 7:35pm On Jan 15, 2016
solution 7 is pending TheTrapezoidal Rule
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 11:15pm On Jan 15, 2016
Otuabaroku:

@Op, I think this is an assignment given to you to do. It makes sense for you to make an attempt. When people see your effort, then you can be helped. In that way,you learn more from the assignment.

I was able to come about this for number 1
int maxmul(int a, int b, int c)

{
int min;
min = a;
if b<min min = b;
if c<min min = c;
return a*b*c/min;
}

Question 2
double inc(double x)
{
return x+1.0;
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 11:17pm On Jan 15, 2016
proxy20:
solution 7 is pending
TheTrapezoidal Rule

I am very grateful thank you alot!............Please teach me! I really want to learn!!
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 11:22pm On Jan 15, 2016
proxy20:
solution 7 is pending
TheTrapezoidal Rule

As regards your solutions, can i present the first like this?

#include<iostream>
int maxmul(int a, int b, int c)
{
}
int main()
{

int a =1;
int b=2;
int b=3;
product =inta*intb

cout <<product<< endl;
return 0
}
cout << maxmul(1,2,3) << endl;
cout << maxmul(3,1,2) << endl; cout << maxmul(3,2,1) << endl;
}
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 11:24pm On Jan 15, 2016
check Google or your math textbook. is it a school assignment.
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Otuabaroku: 5:54am On Jan 16, 2016

Iyalodeofcanada:

I was able to come about this for number 1
int maxmul(int a, int b, int c)
{
int min;
min = a;
if b<min min = b;
if c<min min = c;
return a*b*c/min;
}

Question 2
double inc(double x)
{
return x+1.0;

@Iyalodeofcanada let me put you through to number 1 and 5, with that you can try the rest.

Question 1. Write a function that takes three integers as arguments and returns the product of
the largest two.

Here you are required to find the largest two of the three numbers entered by the user. In my solution, I have decided to store the data entered by the user in array and subsequently sort them in ascending order. With this sorting, I now know that the last two are the largest two of the three numbers entered by the user; hence, simply multiplying the two, will give me the answer.Here is code blow.


#include <iostream>
using namespace std;

int ProductOfLargestTwo(int a,int b,int c)
{
//store the user input with array
int userInput[3] = {a,b,c};

//sort the array in ascending order
for (int i = 0; i < 3; i++)
{
int compare = i+1;
if (compare < 3 && userInput[i]> userInput[compare] )
{
int temp = userInput[i];
userInput[i] = userInput[compare];
userInput[compare] = temp;
}
}

//returns the product of the largest two
return userInput[1] * userInput[2];
}

int main()
{
cout<<"the product of the largest two is :"<<ProductOfLargestTwo(20,2,140)<<endl;
getchar();
return 0;
}

Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Otuabaroku: 6:26am On Jan 16, 2016

For solution 5.

Question 5. Write a function to calculate the average value of a 100 by 100 array of ints.
Return the average as a double.


double AverageValue(int userInput[100][100])
{
// declare the variable to hold the sum and the average values and initialize them to zero
double sum,average = 0;

//get the sum by going through the array adding the values
for (int i = 0;i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
sum += userInput[i][j];
}
}

//sum is divided by 100*100 to get the average because 100*100 will give you the total number of elements in the array.
//for example the total number of elements in 2 by 2 array (int userInput[2][2] = {{1,2,},{5,6}}) is 4 (i.e 2*2).
average = sum/(100*100);

// return the average
return average;
}


Hope this helps to try the others.
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 9:59am On Jan 16, 2016
You can convert this bunch of programs to a native android app using Android ndk and studio
or eclipse with adt bundle ,sdk and ndk to cool apps . NDK is for native apps development in c/c++ . create the java class , and c/c++ class and android Xml and build.
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 5:42pm On Jan 16, 2016
Otuabaroku:



@Iyalodeofcanada let me put you through to number 1 and 5, with that you can try the rest.

Question 1. Write a function that takes three integers as arguments and returns the product of
the largest two.

Here you are required to find the largest two of the three numbers entered by the user. In my solution, I have decided to store the data entered by the user in array and subsequently sort them in ascending order. With this sorting, I now know that the last two are the largest two of the three numbers entered by the user; hence, simply multiplying the two, will give me the answer.Here is code blow.


#include <iostream>
using namespace std;

int ProductOfLargestTwo(int a,int b,int c)
{
//store the user input with array
int userInput[3] = {a,b,c};

//sort the array in ascending order
for (int i = 0; i < 3; i++)
{
int compare = i+1;
if (compare < 3 && userInput[i]> userInput[compare] )
{
int temp = userInput[i];
userInput[i] = userInput[compare];
userInput[compare] = temp;
}
}

//returns the product of the largest two
return userInput[1] * userInput[2];
}

int main()
{
cout<<"the product of the largest two is :"<<ProductOfLargestTwo(20,2,140)<<endl;
getchar();
return 0;
}




Thank you for this but your code seems too advanced for a baby programmer like me(I started this class one week ago). I have been googling each line all day. The truth is it is an assignment due tommorow. It is not as if i am been lazy but right now i just have to deliver and i am looking for the simplest way to go about this later i can now read up and catch up on array declarations and all that. Still working on it though thanks alot


#include <iostream>
using namespace std;
int maxmul(int a, int b, int c)
{

int min;
int a = 1;
int b = 2;
int c = 3;
}

int main()

if b < min min = b;
if c < min min = c;

{
cout << maxmul(1,2,3) << endl;
cout << maxmul(3,1,2) << endl; cout << maxmul(3,2,1) << endl;
}

return a*b*c/min;
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Nobody: 5:48pm On Jan 16, 2016
Which compiler are u using?
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by DonSegmond(m): 6:03pm On Jan 16, 2016
I decided to implement a few of these using clojure.
I like clojure, because it's a lisp that runs on java. by being a lisp, you can develop functional programs that are very small, easy to test and fast. the code below took about 5 minutes to write.



(defn q1 [args]
(let [max_arg (apply max args)
next_max_arg (apply max (remove #(= % max_arg) args))
]
(* next_max_arg max_arg)))

(defn q2 [arg]
(inc arg))

(defn q3 [arr]
(/ (reduce #(+ %1 %2) arr) (count arr)))

(defn q4 [arr]
(let [avg (/ (reduce #(+ %1 %2) arr) (count arr))]
(map #(- %1 avg) arr)))


(println (q1 [2 3 1 10 5 7]))
(println (q2 7.0))
(println (q3 [1 2 3 4 5 6 7 8 9 10]))
(println (q4 [1 2 3 4 5 6 7 8 9 10]))



Results

70
8.0
11/2
(-9/2 -7/2 -5/2 -3/2 -1/2 1/2 3/2 5/2 7/2 9/2)
Re: Help Needed With The Following Questions In C++ Or Any Language Please!!! by Iyalodeofcanada: 6:05pm On Jan 16, 2016
Visual studio c++ 2011.
proxy20:
Which compiler are u using?

(1) (Reply)

Data Analysis Packages: Which Should I Learn?! / Let’s Build a Todo-List Web Application With ES6 Vanilla Javascript / 15 Resources To Learn Python Programming Language

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.