In: Computer Science
1. Solve a function (e .g., y(x) = sin(x) / (sin(x/10) + x/10) for many different values of x between a user-defined min and max, and store the values in a list. Also, print the maximum value of y(x) for the given range.
2. Find the list in a list-of-lists whose sum of elements is the highest, and print the sum and list to the user.
e.g., [[1, 2], [2, 5], [3, 2]] → [2,5] with a sum of 7.
I am writing the code in C++ and also after // this symbol i will explain about that line.
question no. 1:
#include<iostream>
#include<cmath> // this library function will gives sin value
#include<algorithm>
using namespace std;
int main()
{
double max,min,temp=0; // sin value always in double type data
double list[30]; // declaring array for storing the y(x) function value
int l=0;
int n;
cout<<"Enter the minimum value of x ";
cin>>min; //taking data from user input
cout<<"Enter the maximum value of x ";
cin>>max;
for(int x=min;x<=max;x++)
{
temp= sin(x)/(sin(x/10)+x/10); // calcuating y(x) in the range of min to max value
list[l]=temp;
cout<<temp<<endl;
l++;
temp=0;
}
n=sizeof(list)/sizeof(list[0]); // sorting algorithm to sort array in decending order
sort(list, list+n,greater<int>());
cout<<"The maximum value of y(x) is "<<list[0];
return 0;
}
Question no.2:
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
int list[3][2]={{1,2},{2,5},{3,2}}; // declaring list in 2d array
int sum=0;
int temp[3]; // declaring 2nd array for store sum of each pair
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
sum=sum+list[i][j]; // adding the pair
}
temp[i]=sum; //storing the sum into temp array list
sum=0;
}
int n=sizeof(temp)/sizeof(temp[0]);
sort(temp,temp+n); // now here sorting of array increasing order
cout<<"highest sum is "<<temp[2]; now printing n-1 element which will be highest.
return 0;
}