My code is not working because I have a problem with the call of my two functions when I use a structure. I think I didn't call it correctly but I'm not sure if the problem is here or in the definition itself.
Here is my code:
#include <stdio.h>
#define NUM 3
struct student{
char name[20];
int kor;
int math;
int eng;
int sum;
double avg;
double avg2;
double k, m, e;
};
void average(student* st)
{
int i, sum = 0;
for(i=0;i<NUM;i++) {
st[i].sum= st[i].kor + st[i].math + st[i].eng;
st[i].avg= st[i].sum / NUM;
}
}
void average2(student* st)
{
int i, sum = 0;
double K, M, E;
for(i=0;i<NUM;i++) {
K+= st[i].kor;
M+= st[i].math;
E+= st[i].eng;
}
}
int main(void)
{
student stu[NUM]={{"Tom"},{"Jane"},{"Eddy"}} ;
int i;
int max;
double K, M, E;
printf("Input scores.\n");
for(i=0;i<NUM;i++)
{ printf("\n<%s>\n",stu[i].name);
printf("Korean:");
scanf("%d",&stu[i].kor);
printf("Math:");
scanf("%d",&stu[i].math);
printf("English:");
scanf("%d",&stu[i].eng);
}
printf("\nName\tKorean\tMath\tEnglish\tSum\tAverage\n");
average(stu);
for(i=0;i<NUM;i++)
printf("%s\t%d\t%d\t%d\t%d\t%.2f\n",stu[i].name,stu[i].kor,stu[i].math,stu[i].eng,stu[i].sum,stu[i].avg);
average2(stu);
printf("Average %.2lf\t%.2lf%.2lf\n", k/3, m/3, e/3);
}
Thank you in advance for your answers, Coco
for loops should have { and } to enclose more than one line in c++.
for(i=0;i<NUM;i++)
{
st[i].sum= st[i].kor + st[i].math + st[i].eng;
st[i].avg= st[i].sum / NUM;
}
Also in your function average2 it is not clear what you are exactly doing.
You are declaring same variable in main and average2 double K, M, E; so the function will take local variable only.
For your second printf here is the logic.,
for(i=0;i<NUM;i++) {
K+= st[i].kor;
M+= st[i].math;
E+= st[i].eng;
}
printf("Average %.2lf\t%.2lf%.2lf\n", K/3, M/3, E/3);
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.
Here is my code
run time memory will depend on the OS but i don't want that level.just want to solve this type question from basic level.
include <studio.h>
int calculate(int n);
int number = 8;
int main(){
int add;
add = calculate(number);
return 0;
}
int calculate(int x){
if(x==0){
return x;
}else{
return x+calculate(x-1);
}
}
First problem, Use
#include <stdio.h>
instead of
include <studio.h>
Second problem, use x instead of n,
return x+calculate(x-1);
Full code:
#include <stdio.h>
int calculate(int n);
int number = 8;
int main()
{
int add;
add = calculate(number);
printf("%d\n", add);
return 0;
}
int calculate(int x)
{
int n = 0;
if(x==0)
{
return x;
}
else
{
return x+calculate(x-1);
}
}
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);
}
I am writing a pretty simple piece of code for school homework, but assigning one of the functions to a variable throws an undefined refence error.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
void skaitimas (double KKa[], int KKi [],int & n); // KKa - kainos, KKi - kiekiai
void pilnoskainos (double KKa[], int KKi[], double PK[], int & n); // PK - pilnos knygu kainos
double suma (double PK[], int & n);
int kiekisfunkcija (int KKi[] , int & n);
void rasymas (double KKa[], int KKi [], double PK[], double, int, int & n); // sumaats - visu knygu kaina, kiekisats - kiek knygu daugiau negu 5
int main()
{
cout << "Programos Pradzia" << endl;
int x;
ifstream fn ("Duomenys1.txt");
fn>>x;
fn.close();
int KKi[x], kiekisats, n;
double KKa[x], PK[x], sumaats;
skaitimas (KKa, KKi, n);
pilnoskainos (KKa, KKi, PK, n);
sumaats = suma(PK, n);
kiekisats = kiekisfunkcija(KKi, n);
rasymas (KKa, KKi, PK, sumaats, kiekisats, n);
return 0;
}
void skaitimas (double KKa[], int KKi [], int & n)
{
ifstream fn ("Duomenys1.txt");
int i;
fn>>n;
for (i=0;i<n;i++)
{
fn>>KKa[i]>>KKi[i];
}
fn.close();
}
void pilnoskainos (double KKa[], int KKi[], double PK[], int & n)
{
int i;
for (i=0;i<n;i++)
{
PK[i]=KKa[i]*KKi[i];
}
}
double suma (double PK[], int & n)
{
double sumaats;
int i;
for (i=0;i<n;i++)
{
sumaats = sumaats + PK[i];
}
return sumaats;
}
int kiekis (double PK[], int & n)
{
int daugiaunegu5 = 0, i;
for (i=0;i<n;i++)
{
if (PK[i]>5) daugiaunegu5++;
}
return daugiaunegu5;
}
void rasymas (double KKa[], int KKi [], double PK[], double sumaats, int kiekisats, int & n)
{
ofstream fr ("Rezultatai1.txt");
int i;
for (i=0;i<n;i++)
{
fr<<KKa[i]<<" "<<KKi[i]<<" "<<PK[i]<<endl;
}
fr<<sumaats;
fr<<kiekisats;
}
And here is the error:
|23|undefined reference to `kiekisfunkcija(int*, int&)'|
Can anyone please help me
The function is declared
int kiekisfunkcija (int KKi[] , int & n);
but is not defined. You forgot to define the function.