How can I use variables in C++ without passing into a function - c++

Is there a way in C where you can use variables that you don't pass in variables?
I'm working on some code that someone else started on and I'm just modifying it.
Here is some code that I have.
#include <iostream>
using namespace std;
void rotate90() {
for (int w=0;w<n;w++) {
for (int h=0;h<n;h++) {
chart2[h][w]=chart[n1-w][n1-h];
}
}
for (int h=0;h<n;h++) {
for (int w=0;w<n;w++) {
chart[h][w]=chart2[h][w];
}
}
}
int main() {
......
else
{
rotate90;
}
}
}

Related

Reference to a non-const object cannot be initialized with an r-value of that object

I create an object and try to pass that object through multiple function by reference
#include<iostream>
#include <string>
#include "DBMS.h"
using namespace std;
void Home(DBMS &);
int main()
{
DBMS dbms();
Home(dbms); // this is where the error is
return 0;
}
void Home(DBMS &dbms)
{
string dbName;
dbms.addDatabase(dbName);
}
and this is DBMS.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Database.h"
using namespace std;
class DBMS
{
public:
DBMS();
void addDatabase(string);
Database& getDatabase(string);
Database& getDatabaseByIndex(int);
int getDatabaseIndex(string);
void removeDatabase(string);
int size();
~DBMS();
private:
vector <Database> dbList;
string error;
};
DBMS::DBMS()
{
}
void DBMS::addDatabase(string tbNames)
{
vector<string> TB_Names = tokenize(tbNames);
int size = TB_Names.size();
for (int i = 0; i < size; i++)
{
if (getDatabaseIndex(TB_Names[i]) == -1)
{
Database tb(TB_Names[i]);
dbList.push_back(tb);
}
else
throw "Already esited Database with given name";
}
}
Database& DBMS::getDatabase(string tbName)
{
int i;
int size = dbList.size();
for (i = 0; i < size; i++)
{
if (dbList[i].getName() == tbName)
return dbList[i];
}
throw invalid_argument("Database not found");
}
Database& DBMS::getDatabaseByIndex(int index)
{
return dbList[index];
}
void DBMS::removeDatabase(string TB_Name)
{
int i;
int size = dbList.size();
for (i = 0; i < size; i++)
{
if (dbList[i].getName() == TB_Name)
{
dbList.erase(dbList.begin() + i);
break;
}
}
if (i == size)
throw invalid_argument("Field not found");
}
int DBMS::getDatabaseIndex(string TB_Name)
{
int size = dbList.size();
for (int i = 0; i < size; i++)
{
if (dbList[i].getName() == TB_Name)
return i;
}
return -1;
}
int DBMS::size()
{
return dbList.size();
}
DBMS::~DBMS()
{
}
(Database type is just another class i create. Nothing special about it. Don't worry about it (Unless you think I have to))
The error statement is : >a reference of type "DBMS &" (a non-const-qualified) cannot be initialized with an value of "DBMS()"
I found a suggestion that i should fix void Home(DBMS &dbms) to void Home(const DBMS &dbms)
but if i do that, i can't use addDatabase() function
How can i fix this?
This declaration
DBMS dbms();
is a vexing parse. You are not declaring a variable named dbms, but instead you are actually declaring a function named dbms that takes no arguments, and returns a DBMS. Passing this object to a function expecting a DBMS object will not work.
You can fix this with:
DBMS dbms{};

how can i count digits of a number using recursion?

yes i know other ways to count digits and returning to main function from the recursion function, but i'd like to print it in the void function. im having difficulty with it. could somebody help?
#include <iostream>
using namespace std;
void recursive_function(int num)
{
int sum=0;
while(num!=0){
recursive_function(num/10);
sum++;
}
cout<<sum<<endl;
}
int main()
{
recursive_function(345289467);
return 0;
}
If you do not want to use the return-stack to count your digits, you will need to pass the current count throughout the call stack as function parameter:
void recursive_function(int num, int count=1)
{
if (num>=10) {
recursive_function(num/10, count+1);
} else {
cout<<count<<endl;
}
}
your recursive function should return integer.
#include <iostream>
using namespace std;
int recursive_function(int num)
{
if(num>9){
return 1+recursive_function(num/10);
}else
return 1;
}
int main()
{
cout << recursive_function(123456789);
return 0;
}

Error message saying Runtime Error (SEG) keeps coming up

I submitted my solution for a problem( http://opc.iarcs.org.in/index.php/problems/WORDLIST ) to the site .It is served by an online judge.Every time i submit it, it says runtime error and shows the following message :
Runtime Error: SEG
Description: Segmentation fault
Runtime: 0
What is this fault and how do i fix it?
Note:My knowledge of c++ is intermediate and i would be grateful if you could explain it in a simple way
This is my code for the problem:
#include<iostream>
#include<ctype.h>
#include<string.h>
using namespace std;
class problem
{
public:
int nol,k,j,w,cp;
char inp[1000][80],qed[1000][80];
problem()
{
k=j=w=cp=0;
}
void input()
{
cin>>nol;cin.get();
for(int i=0;i<nol;i+=1)
{
cin.getline(inp[i],80);
cin.clear();
}
cout<<endl;
lineprocess();
}
void lineprocess()
{
if(k<nol)
wordprocess();
else
display();
}
void wordprocess()
{
for(;;)
{
if(ch(inp[k][cp]))
{qed[w][j]=0;break;}
else
{
qed[w][j]=tolower(inp[k][cp]);
j+=1;cp+=1;
}
}
decide();
}
void decide()
{
if(inp[k][cp]==0)
{
w+=1;
cp=0;
j=0;
k+=1;
lineprocess();
}
else
{
w+=1;
j=0;
cp+=1;
wordprocess();
}
}
int ch(char a)
{
if(!isalpha(a))
return 1;
else return 0;
}
void display()
{
char dumm[80]="";
qed[w][0]='\0';
sortarray();
removerep();
for(int i=0;i<=w;i+=1)
if(strcmp(qed[i],dumm)!=0)
if(strcmp(qed[i],qed[i+1])!=0)
cout<<qed[i]<<endl;
}
void sortarray()
{
char ub[80];
for(;checksort();)
{
for(int q=0;q<w;q++)
{
if(strcmp(qed[q],qed[q+1])>0)
{
strcpy(ub,qed[q]);
strcpy(qed[q],qed[q+1]);
strcpy(qed[q+1],ub);
}
}
}
}
int checksort()
{
int t=0;
for(int i=0;i<w;i+=1)
{
if(strcmp(qed[i],qed[i+1])>0)
{t+=1;break;}
}
return t;
}
void removerep()
{
int nodw=0;
for(int i=0;i<w;i+=1)
if(strcmp(qed[i],qed[i+1])!=0)
nodw+=1;
cout<<nodw<<endl;
}
};
int main()
{
problem r2d2;
r2d2.input();
return 0;
}
From the linked page:
You may assume that N ≤ 10000
Since you are using
char inp[1000][80],qed[1000][80];
you are likely accessing memory beyond the valid range, which will cause you problems. Change them to:
char inp[10000][80],qed[10000][80];

no output,I have make a program to find all armstrong no.between 100 to 1000

#include<iostream>
using namespace std;
int main() {
int a,b,c,d;
c=0;
for(a=100;a<1000;a++) {
for(b=a;b>0;b=b/10) {
d=b%10;
c=c+d*d*d;
}
if(c==a) {
cout<<c<<endl;
}
}
return 0;
}
You have to initialize c before each check.
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
c=0;
for(a=100;a<1000;a++)
{
c=0; // add this
for(b=a;b>0;b=b/10)
{
d=b%10;
c=c+d*d*d;
}
if(c==a)
{
cout<<c<<endl;
}
}
return 0;
}

This code shows error "stu undeclared"?? what should i do

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)