I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)
Related
I am trying to call this recursive method,but I don't understand why it's not reaching the else statement to return mul. I am passing value 0,in the main function,which is less than n,and on x becoming greater than n,it should move to else function.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n,mul=0;
vector <int> a;
int calc(int x)
{ int mul,chk;
int l=n;
if(x<n)
{
chk=a[x]*(l-1);
l--;
if(mul<chk)
{
mul=chk;
}
calc(x+1);
}
else
{ return mul;}
}
int main() {
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
int z=calc(0);
cout<<z;
return 0;
}
Wihtout knowing the exact error message, I am not 100% sure what is the problem, but I guess it is the following:
Your function looks like this (simplified):
int calc(int x) {
if(someCondition){
if(otherCondition){}
calc(x+1);
} else {
return mul;
}
}
The problem is that if someCondition is true, the function does not return anything. You probably want to do:
return calc(x+1);
instead of just
calc(x+1);
I've been recently working on a program which consists basically of 24 variations of one function(below). Everything gets executed perfectly apart from the part where I try to compare functions(with eachother). I found out that it is possible to be done by writing 24 if-else statements, yet I am certain there is a shorter way. I've also tried with vectors but no luck for now. Thanks for any help!
one of 24 functions:
int funk1()
{
ifstream myfile ("file.txt");
string line;
int i;
class1 obj1;
obj1.atr1= "Somename";
obj1.atr2="GAATTC";
while (getline(myfile, line))
{
i = countSubstring(line, obj1.atr2);
obj1.sum += i;
};
cout<<obj1.sum<<": "<<obj1.atr1<<"\n";
return obj1.sum;
}
The main function:
int main(){
funk1();
funk2();
funk3();
funk4();
funk5();
funk6();
funk7();
funk8();
funk9();
funk10();
funk11();
funk12();
funk13();
funk14();
funk15();
funk16();
funk17();
funk18();
funk19();
funk20();
funk21();
funk22();
funk23();
funk24();
//This is one way to do it
if (funk18() > funk1())
{
cout<<funk18<<" is the biggest";
}
//...
}
Here is a clean and elegant c++11 solution:
#include <iostream>
#include <functional>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
using MyFunc = std::function<int()>;
int f1() { return 1; }
int f2() { return 15;}
int f3() { return 3; }
int main() {
std::vector<MyFunc> my_functions = {f1, f2, f3};
int max = std::numeric_limits<int>::min();
for (auto const &f : my_functions) {
max = std::max(max, f());
}
cout << max << endl;
return 0;
}
if you want to store the results from functions instead, you could do:
std::vector<int> my_results;
my_results.reserve(my_functions.size());
for (auto const &f : my_functions) {
my_results.push_back(f());
}
auto max_it = std::max_element(std::begin(my_results), std::end(my_results));
cout << *max_it << endl;
i tried to make a program on seperate files. Unfortunatelty i had erros while trying to build the code. It was pointing on undefined references to constuctors,destructos and function CzynnikiPierwsze. So i decied to put the whole code in one code. Still there is a problem in main() function: undefined reference to 'CzynnikiPierwsze(int)' Any ideas whats wrong? Here is the whole code:
#include <iostream>
#include <cctype>
#include <vector>
using namespace std;
vector<int> CzynnikiPierwsze(int);
class NieprawidlowaDana //wyjatki
{};
class SpozaZakresu
{};
class RozkladLiczby{
private:
int *tab;
public:
RozkladLiczby(int); //konstruktor
vector<int> CzynnikiPierwsze(int); //metoda
~RozkladLiczby();
};
/////////////////BODY of the CLASS/////////////////////////////////////
RozkladLiczby::~RozkladLiczby() //destruktor
{}
RozkladLiczby::RozkladLiczby(int n){
int* tab = new int[n+1];
int i,j;
for( i=0;i<=n;i++)
tab[i]=0; //zerujemy tablice
for( i=2;i<=n;i+=2)
tab[i]=2; //zajmujemy sie liczbami parzystymi
for(i=3; i<=n;i+=2)
for(j=i;j<=n;j+=i) //sito erastotesa
if(tab[j]==0)
tab[j]=i;
}
vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
vector<int> tablica;
while(m!=1){
tablica.push_back(tab[m]);
m=m/tab[m];
}
return tablica;
}
////////////////////////END OF THE BODY//////////////////////////////
int parsuj(char* argz){
int i=0;
while(argz[i] != '\0'){ //funckja ktora konwertuje na int i sprawdza czy wprowadzaony zostal string
if( !isdigit(argz[i]))
throw NieprawidlowaDana();
i=i+1;
}
int x = stoi(argz);
if (x >= 2)
return x;
else
throw SpozaZakresu();
}
//////////////////GLOWNY BLOK///////////////////////////////////////
int main(int argc,char* argv[]){
vector<int> tablica,p;
int i,n;
int max;
for( i=1;i<=argc-1;i++){
n = parsuj(argv[i]);
tablica.push_back(n);
}
max=tablica[0];
for(i=1; i<=argc-1;i++){
if(tablica[i]>max)
max=tablica[i]; } // sprawdzamy max
RozkladLiczby odp = RozkladLiczby(max); //utwoorzylismy obiekt z maxa
for(unsigned int i=0;i<=tablica.size()-1;i++){
p=CzynnikiPierwsze(tablica[i]);
cout<<tablica[i]<<" = ";
int x= p[0];
int licznik = 1;
for(unsigned int j=1;j<=p.size()-1;j++){
if(x==p[j])
licznik++;
else if(licznik!=1)
cout<<x<<"^"<<licznik<<"*";
else
cout<<x<<"*";
}
cout<<endl;
}
return 0;
}
I would be grateful if u could solve this.
You have declared global function vector<int> CzynnikiPierwsze(int); but you have not defined it anywhere in your program. In your main you are calling global function and not the one which is your class member.
I'm new on programming and I need to learn it for Arduido purposes. I used this code to test some things but I keep getting the "expected primary function before "int"" error, and it also says that the position functions isn't declared.
Am I declaring it wrong? I've tried many different things and kept getting the same message. My objective is to keep typing '1' and get 3, 6, 9, etc on the screen from calling the position function at cout.
#include <iostream>
using namespace std;
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
// this is where I get the error //
int position()
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
cout << position();
}
return 0;
}
Nesting functions is not allowed in C++. Change your code to be:
#include <iostream>
using namespace std;
int main()
{
// code
}
int position()
{
}
The problem is that your function position is in your function main, which is not possible in c++. Move the position out of the main function.
int position(int r, int degree)
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
cout << position(r, degree);
}
return 0;
}
I was wondering why do I keep getting an error and unable to return an array;
also, once the sell_item function actually work and return an array..how do I echo that array from the main function.
thanks
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int itemnum = 3333;
string itemName="Cooking Range";
int Qauntity=1;
int NumberOfItems=2;
int NumberOfFields=5;
double function_Sell_Item(int itemnum,string itemName, int Qauntity);
int main () {
function_Sell_Item(itemnum, itemName, Qauntity);
}
double function_Sell_Item(int itemnum,string itemName, int Qauntity) {
double arraylist[2][5];
for (int index =0; index < NumberOfItems; index++) {
for (int i=0; i < NumberOfFields; i++) {
arraylist[index][i]=0;
}
}
return arraylist;
}
//// functions ends
:
;
You're trying to return an array, which you cannot do in C++.
You should consider something like std::vector<double>, because you can return that.
typedef std::vector<double> MyVec;
MyVec foo() {
MyVec v;
v.push_back(3.142);
v.push_back(2.718);
return v;
}
int main() {
MyVec z = foo();
for (int i = 0; i < z.size(); i++) {
std::cout << z[i] << "\n";
}
return 0;
}
Your code wont work at all as you return a local array, whose memory is not valid anymore after function return. You have to allocate the memory for the array dynamically on the heap.
Furthermore you have to change the return type of your function to double **
You are trying to return a double array (two-dimensional) which you can't in C++.
Use Vector instead!