To find product of two largest no - visual-c++

#include<iostream>
#include<vector>
using namespace std;
int max(const vector<int>& num,int n)
{
int n_index=-1;
int m_index=-1;
for(int i=0;i<n;i++)
{
if(m_index==-1||num[m_index]<num[i])
m_index=i;
}
for (int i=0;i<n;i++)
{
if((i!=m_index) && (n_index==-1)||(num[n_index]<num[i]))
n_index=i;
}
int product=num[n_index]*num[m_index];
cout<<"output "<<num[m_index]<<" "<<num[n_index];
cout<<"product "<<product;
}
int main()
{
int n;
cout<<"enter the no ";
cin>>n;
vector<int>num(n);
for(int i=0;i<n;i++)
cin>>num[i];
max(num,n);
}
In the 2nd for loop in my max function after replacing "i" by "j" my code is working but if i use "i" ,why is it not working as "i" is local to that for loop ??

I have tried your code. It seems that the max function should be the non-return type.
void max(const vector<int>& num,int n)
{
...
}
And the program will work with 'i'.

Related

terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not validAborted

this is my code ,the code is written to calculate all possible codes that can be generated from the given string. using recursion.
if input is 1123 it should print all codes as
aabc
kbc
alc
aaw
kw
#include<bits/stdc++.h>
#include <string.h>
using namespace std;
int getCodes(string input, string output[10000]) {
if(input.size()==0)
{
output[0]="";
return 1;
}
int x=input[0]-'0';
char ch='a'+x-1;
int a=getCodes(input.substr(1),output);
for(int i=0;i<a;i++)
{
output[i]=ch+output[i];
}
int mul=10,x2=0;
for(int i=0;i<2;i++)
{
x2=x2+(input[i]-'0')*mul;
mul=mul/10;
}
string output2[1000]={0};
int b=0;
if(x2>10 && x2<=26)
{
char ch2='a'+x2-1;
b=getCodes(input.substr(2),output2);
for(int i=0;i<b;i++)
{
output[a+i]=ch2+output2[i];
}
}
return a+b;
}
int main(){
string input;
cin >> input;
string output[10000];
int count = getCodes(input, output);
for(int i = 0; i < count && i < 10000; i++)
cout << output[i] << endl;
return 0;
}
i'm trying to solve this problem using recursion where ive devided string into a parts where the single char is handle by code and all other are passed for recursion to take care of in another case first two chars are handled by me and remaining are passed to a recursion and finally all are combined

Hi, I wrote code in C++ fot linear search but it displays the index of searched elemet twice with second index value as random garbage value

Here is the code.what i did is implement linear search on some elements of the array and the push searched elements in stack,afterwards I print the popped elements from stack and print them.But in search function it displays two index values.
using namespace std;
int searched[10];
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
int main(void)
{
int arr[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int x = 0;
for(int i=0;i<8;i++)
{
x++;
int result = search(arr, n, x);
cout << "searched Element is " << result<<"\t\t";
push(result);
pop();
}
return 0;
}```
There are two issues that lead to this confusing result.
First, if I am not mistaken the search function, which was written like this:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
is parsed similarly to the following:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout<<"The element is found at the index"<<i<<"\n\n";
}
}
return x;
}
Presumably you meant this:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
}
}
Second, since the global n starts at the value 100, the search loop runs off the end of the length 15 array, into other memory. This is probably undefined behavior.

String not recognized in function definition

I am trying to code the number of occurences of 'e' in a particular string but it is giving me error while defining the function as it says string is not defined in this scope and occurence_e(which is my user-def function) to be not declared in this scope as well.
Following is the code:
#include<iostream>
#include<string.h>
void occurence_e(string);
using namespace std;
int main(){
string input;
cout<<"Enter any name"<<endl;
cin>>input;
occurence_e(input);
return 0;
}
void occurence_e(string input){
int count=0;
for (int i = 0; i < input.length(); i++)
{
if(input.at(i)=='e'){
count++;
}
}
cout<<"No. of times e comes in this name is "<<count;
}
The forward reference uses string, so move using namespace std; above it.
using namespace std;
void occurence_e(string);

I'm not Sure why the incompatible intializer is not compatible with the parameter type int

Basically, I want to display my test scores and the average of them but I am unable to because of these errors
I've tried to take void display and put it in the class and declare it in main but that didn't work
#include <iostream>
#include <string>
using namespace std;
class TestScore
{
public:
TestScore() {};
TestScore(int arr[], int SIZE) {};
void testAvg(int arr[], int SIZE);
void displayArray(int arr[], int Size);
};
void TestScore::testAvg(int arr[], int SIZE)
{
int sum = 0;
for (int i = 0; i < SIZE; i++)
{
sum = sum + arr[i];
try
{
if ((arr[i] > 100) || (arr[i] < 0))
{
throw(1);
}
}
catch (int n)
{
cout << "Error" << endl;
}
}
int average = sum / SIZE;
}
void TestScore::displayArray(int arr[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
cout << arr[i] << endl;
}
}
void main()
{
const int SIZ = 5;
int Grade[SIZ] = { 89,65,99,100,81 };
TestScore T(int Grade, int SIZ);
T(Grade, SIZ).testAvg(Grade, SIZ);
T(Grade, SIZ).displayArray(Grade, SIZ);
system("pause");
}
I expect it to display the average of my score so basically, I want to have an array of 5 test scores displaying and then the average of them.
TestScore T(int Grade, int SIZ); declares a function named T, taking two parameters of type int. You then call that function, passing a parameter of type int[5] - not an int. Hence the error message.
Further, that function is not implemented anywhere. Frankly, I don't understand what you are trying to do with that function declaration; it makes little sense.

Creating a 2 dimensional character array dynamically in C++

I want to create a 2 dimensional character array dynamically through pointers. Then input 10 strings in it and then take a string target from user and find it in array. if it is present then return its index. I have written code for it but it has errors. Please help me in correcting it. Thanks in advance.
#include<iostream>
#include<string>
using namespace std;
int strsearch(char [][50],char *);
int main()
{
char str[10][50];
char *target=new char [50];
int index;
for(int i=0; i<10; i++)
{
str = new char* [50];
str++;
}
for(int i=0; i<10; i++)
{
cout<<"Enter a sting";
cin>>str[i][50];
str++;
}
cout<<"Enter a string to find:";
cin>>target;
index=strsearch(str,target);
if(index<0)
{
cout<<"String not found";
}
else
{
cout<<"String exist at location "<<index<<endl;
}
return 0;
}
int strsearch(char string[10][50],char *fstr)
{
int slen;
for(int i=0;i<10;i++)
{
slen=strlen(**string);
if (strnicmp(*string[50],fstr,slen)== 0)
{
return i;
}
}
return -1;
}
Simply use:
std::vector<std::string> obj;
It will save you all the head & heart aches and guard you against easy to go wrong manual memory management issues. What you are trying to do is to solve the problem C way. With C++ the correct way to do it is using a vector of strings.
I think this is an error in any case:
for(int i=0;i<10;i++)
{
slen=strlen(**string);
if (strnicmp(*string[50],fstr,slen)== 0)
{
return i;
}
}
Must be something like:
for(int i=0;i<10;i++)
{
slen=strlen(string[i]);
if (strnicmp(string[i],fstr,slen)== 0)
{
return i;
}
}
I have done some correction, i think it can help you but i have not compiled to check for errors.
#include<iostream>
#include<string>
#define DIM_1 10 // Avoid to use "magic numbers" in your code
#define DIM_2 50
using namespace std;
int strsearch(char **string,char *fstr);
int main()
{
char **str = new char*[DIM_1]; //char str[10][50]; dynamically allocated array.
char *target=new char [DIM_2];
int index;
for(int i=0; i<DIM_1; i++)
{
str[i] = new char[DIM_2]; //Do not lost the original pointer
//str++;
}
for(int i=0; i<DIM_1; i++)
{
cout<<"Enter a sting";
cin>>str[i][DIM_2];
//str++; Do not lost the original pointer
}
cout<<"Enter a string to find:";
cin>>target;
index=strsearch(str,target);
if(index<0)
{
cout<<"String not found";
}
else
{
cout<<"String exist at location "<<index<<endl;
}
// Free memory!!
for (int i=0; i<DIM_1;i++) delete[] str[i];
delete[] str;
delete[] target;
return 0;
}
int strsearch(char **string,char *fstr) //its dinamicly allocated array
{
int slen;
int result=-1; //Only one return-> structured programming
for(int i=0;i<DIM_1;i++)
{
slen=strlen(**string);
//strlen and strnicmp is C, not C++, check string class.
if (strnicmp(string[i],fstr,DIM_2)== 0) //Find in the string[i]
{
result= i;
}
}
return result;
}

Resources