here is my code. wriiten for to to find armstrong, factorial etc. now i want readNo method private what should i do..?
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Num_Demo
{
public:
int num;
void readNo(int no)
{
num=no;
}
int Factorial (int a)
{
if(a!=0)
{
int f=1;
for (int i=1;i<=a;i++)
{
f=f*i;
}
return f;
}
else
{
return 0;
}
}
int Reverse(int b)
{
int rev=0,rem;
while(b!=0)
{
rem=b%10;
rev=(rev*10)+rem;
b=b/10;
}
return rev;
}
void Palindrome (int c)
{
int num;
int rev=0,rem;
num=c;
while(c!=0)
{
rem=c%10;
rev=(rev*10)+rem;
c=c/10;
}
if(num==rev)
{
cout<<" Number Is Palindrome";
}
else
cout<<" Number is Not Plaindrome";
}
void Armstrong (int d)
{
int sum=0,n1,copy;
copy=d;
while(d!=0)
{
n1=d%10;
sum=sum+n1*n1*n1;
d=d/10;
}
if(sum==copy)
{
cout<<" Number Is Armstrong";
}
else
cout<<" Number is Not Armstrong";
}
};
int main()
{ clrscr();
Num_Demo nd1,nd2,nd3,nd4;
int n1,n2,n3,n4;
cout<<"\n\nEnter The Number To Find Factorial\t";
cin>>n1;
nd1.readNo(n1);
cout<<" The Factorial Is\t"<<nd1.Factorial(n1);
cout<<"\n\nEnter The Number To Find Reverse Number\t";
cin>>n2;
nd2.readNo(n2);
cout<<" The Reverse Is\t"<<nd2.Reverse(n2);
cout<<"\n\nEnter The Number To Find Palindrome\t";
cin>>n3;
nd3.readNo(n3);
nd3.Palindrome(n3);
cout<<"\n\nEnter The Number To Find Armstrong\t";
cin>>n4;
nd4.readNo(n4);
nd3.Armstrong(n4);
getch();
return 0;
}
now i want to make readNo method private. what should i do..? whent i place readNo outside public error "readNo not accessible" pop up. please help me.
You don't use a private function in main function. It's unacceptably.
You put function under private: tag in class definition, but then you can't call it outside from your class, soo then you need public function which will call private function in class.
Related
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
class knapsack
{
public:
int profit[4]={280,100,120,120};
int weight[4]={40,10,20,24};
int total=60;
float fraction[];
int fractotal=0;
int profittotal=0;
int getprofit=0;
float temp[4]={0,0,0,0};
float temp1=0;
float temp2=0;
float temp3=0;
float temp4=0;
void displayvalues()
{
int i;
cout<<"The profit and weight of the item"<<"\n";
cout<<"The Profit"<<" ";
for(i=0;i<4;i++)
{
cout<<profit[i]<<" ";
}
cout<<"\n"<<"The weights"<<" ";
for(i=0;i<4;i++)
{
cout<<weight[i]<<" ";
}
}
float fractionalknapsack()
{
int i,j;
for(i=0;i<4;i++)
{
fraction[i]=profit[i]/weight[i];
}
cout<<"\n"<<"The values are"<<"\n";
for(i=0;i<4;i++)
{
profittotal=profittotal+profit[i];
}
for(i=0;i<4;i++)
{
cout<<fraction[i]<<"\n";
fractotal= fractotal+fraction[i];
}
if(fractotal<=total)
{
cout<<"The maximum possible value is"<<profittotal;
}
else if(fractotal>=total)
{
for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
if(fraction[i]<fraction[j])
{
temp2=fraction[i];
fraction[i]=fraction[j];
fraction[j]=temp2;
temp3=profit[i];
profit[i]=profit[j];
profit[j]=temp2;
temp4=weight[i];
weight[i]=weight[j];
weight[j]=temp4;
}
}
}
for(i=0;i<4;i++)
{
while(getprofit<total)
{
if(getprofit+weight[i]<=total)
{
temp[i]=1;
getprofit+=weight[i];
}
else
{
temp[i]=(total-getprofit)/weight[i];
getprofit=total;
i+=1;
}
}
cout<<temp;
}
}
for(i=0;i<4;i++)
{
temp1+=profit[i]*temp[i];
}
cout<<temp1;
}
};
int main()
{
knapsack k;
k.displayvalues();
k.fractionalknapsack();
}
So here if you see i have am solving fractional knapsack question with my approach but there is a error in sort operation so can someone give me the solution for this. It would really helpfull. So in the function of fractional knapsack you will se a else if where I have to do sort if the next value to the previous value is greater i used sort operation but is not working. So someone can help me with this.
My program is supposed to take the names and 5 marks each for 10 students and output their corresponding average marks and grade. My functions avgmarks and avg_grade are supposed to calculate and return these, as arrays, to the main function. Using the statements return avg[10] and return gr[10] does not work. I know pointer variables can be used here, but i don't know how to implement them to make my program work correctly.
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
int readmarks(int p[10][5]);
int avgmarks(int p[10][5]);
char avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int avg_marks[10];
avg_marks[10]=avgmarks(marks);
char grade[10];
grade[10]=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
int readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int avgmarks(int p[10][5])
{
int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg[10];
}
char avg_grade(int p[10])
{
char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr[10];
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Arrays cannot be returned from functions. It's one good reason to use vectors instead of arrays (there are many more).
But if you want to carry on with arrays you should use pointers to simulate returning an array from your function.
In this example avg is a pointer to an array that will recieve the returned value
void avgmarks(int p[10][5], int* avg)
{
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
}
You use it like this
int avg_marks[10];
avgmarks(marks, avg_marks);
PS. You seem to have the common (but to me bizarre) newbie misunderstanding that if you have an array of size 10 (say) then array[10] can be used to refer to the whole array. Please don't think that, if you have an array of size 10, then array[10] is just an error, because the valid indexes for an array of size 10 are 0 to 9.
This example changed every c-style array to std::array. Keep in mind, that it is the same code as yours with the arrays, so every programming error is still in there. And also arrays are returned by value, means that they are copied. Try to understand how to use std::array and you will get a better programmer.
#include <iostream>
#include <string>
#include <array>
using namespace std;
void readnames(std::array<string, 10>& p);
void readmarks(std::array<std::array<int,5>, 10>& p);
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p);
std::array<char,10> avg_grade(std::array<int,10>& p);
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g);
int main()
{
std::array<string, 10> names;
readnames(names);
std::array<std::array<int,5>, 10> marks;
readmarks(marks);
std::array<int,10> avg_marks;
avg_marks = avgmarks(marks);
std::array<char,10> grade;
grade = avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(std::array<string, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(std::array<std::array<int,5>, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p)
{
std::array<int,10> avg;
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
std::array<char,10> avg_grade(std::array<int,10>& p)
{
std::array<char,10> gr;
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g)
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
I think this will work:
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
void readmarks(int p[10][5]);
int* avgmarks(int p[10][5]);
char* avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int *avg_marks;
avg_marks=avgmarks(marks);
char *grade;
grade=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int* avgmarks(int p[10][5])
{
static int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
char* avg_grade(int *p)
{
static char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Basically, to return an array from a function in C/C++, you declare it as "static", and you make the function return the pointer.
EDIT: As many others have noted, this won't work in multi-threaded programs.
Run the code to see where the problem lies.
[Error] 'd' does not name a type
I tried declaring the variable directly in public but it kept popping up with more errors.
There seems to be apparently no fix, since googling didn't help much in the process.
It'd be super helpful if someone could help me figure out where the problem is.
#include<iostream>
using namespace std;
class array1{
protected:
static int a[50];
public:
int n1;
void getNum1(void)
{
cout<<"how many numbers?"<<endl;
cin>>n1;
for(int i=0;i<n1;i++)
{
cout<<"enter le number"<<endl;
cin>>a[i];
}
}
int* retNum1(void)
{
return a;
}
};
class array2{
protected:
static int b[50];
public:
int n2;
void getNum2(void)
{
cout<<"how many numbers?"<<endl;
cin>>n2;
for(int i=0;i<n2;i++)
{
cout<<"enter le number"<<endl;
cin>>b[i];
}
}
int* retNum2(void)
{
return b;
}
};
class conarray:public array1, public array2{
private:
int c[100],d;
//int d;
public:
d=0;
void disp()
{
for(int i=0;i<5;i++)
{
cin>>c[i];
}
for(int i=0;i<5;i++)
{
cout<<c[i]<<endl;
}
cout<<d;
}
//int d = 0;
/*void merge(void)
{
int *nn1 = retNum1();
int *nn2 = retNum2();
for(int i=0;i<n1;i++)
{
c[d]=nn1[i];
d++;
}
for(int i=0;i<n2;i++)
{
c[d]=nn2[i];
d++;
}
}
void display(void)
{
cout<<"NUMBERS IN ARRAY:"<<endl;
for(int i=0;i<d;i++)
{
cout<<c[i]<<endl;
}
}*/
};
int main()
{
conarray a;
//a.getNum1();
//a.getNum2();
//a.merge();
//a.display();
a.disp();
return 0;
}
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
#include<iostream>
using namespace std;
class ulam
{
int num;
double prod;
int cot;
public:
ulam(){cot=0;}
ulam(int x)
{
num=x;
}
void process()
{
for(int i=0;num==1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
prod=num/2;
}
else
{
prod=(3*num)+1;
}
num=prod;
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
}
when i write this code the cot value comes as a garbage value i think. i cant figure out where i went wrong. i used class because i feel it is more discriptive . but the main aim behind this program is to find the number of steps it is required for a number to reach one and to print the whole sequence of numbers. for thos who dont know about the collatz conjecture https://en.wikipedia.org/wiki/Collatz_conjecture
Your condition of the for loop inside process function is wrong. it should be num!=1. You need to initialize cot too. You don't need prod actually.
#include<iostream>
using namespace std;
class ulam
{
int num;
int cot;
public:
ulam()
{
cot=0;
}
ulam(int x)
{
cot=0;
num=x;
}
void process()
{
for(int i=0;num!=1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
num=num/2;
}
else
{
num=(3*num)+1;
}
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
return 0;
}
First Problem
In the constructor where you initialize when an integer is passed, you ALSO have to initialize cot like this:
ulam(int x)
{
cot = 0;
num = x;
}
Better yet, since cot is always going to be 0 at construction, just set cot to 0 as a private variable like this:
class ulam
{
int num;
double prod;
int cot = 0;
public:
//....
};
This means that you could still have a default constructor that will do nothing, and the one that takes an integer won't require cot to be set to 0.
Second Problem
Your second problem is that the loop condition is wrong. It should be num != 1, not num == 1. num == 1 would be the loop would never run unless 1 was passed in cin.