Sorting of structures using elementary methods - c++

This is the program which totals the marks of each student and sorts the total but does not swap the order of other parameters such as student name, and their subject marks. How to sort the structure as a whole while keeping the total marks as basis for sorting? I don't want to use any built in functions and do it by elementary methods.
#include<stdio.h>
#include<conio.h>
struct stnd
{
int sub[20];
char name[20];
int total;
}
stnd[20];
main()
{
int i, j, n=4, m=4,k;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
scanf("%d",&stnd[i].sub[j]);
for(i=0; i<n; i++)
scanf(" %s",stnd[i].name);
for(i=0; i<n; i++)
{
stnd[i].total=0;
for(j=0; j<m; j++)
stnd[i].total=stnd[i].total+stnd[i].sub[j];
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(stnd[i].total<stnd[j].total)
{
k=stnd[i].total;
stnd[i].total=stnd[j].total;
stnd[j].total=k;
}
}
}
printf("Rank\t Chin\t Math\t Eng\t Comp\t total\t name\n");
for(i=0; i<n; i++)
{
printf("%d\t",i+1);
for(j=0; j<m; j++)
printf("%d\t",stnd[i].sub[j]);
printf("%d\t",stnd[i].total);
printf("%s\t\n",stnd[i].name);
}
getch();
}

In the function where you're swapping, just swap the structure instead of the total:
// Where you declare k, declare it as a struct stnd
struct stnd k;
// Where you swap, just swap the structures, not the totals
k = stnd[i];
stnd[i] stnd[j];
stnd[j] = k;
When you set a struct stnd, it does a bitwise copy of the object you're copying, which is exactly what you need for the sorting.

Related

Output not getting print after matrix multiplication

In the code below, my aim is to multiply two matrices reflect[3][3] and mat[3][s] where s can be any value 0-10. Here the statement (A) and (B) is not getting printed, please tell me why??
#include<iostream>
# include<math.h>
#include<conio.h>
using namespace std;
int mat[10][10];
int result[3][10];
int reflect[3][3]= {1,0,5,0,1,5,0,0,1};
int i , j,k,s;
void multiply_matrix(int A[3][3], int B[3][10])
{
for(i=0; i<3; i++)
for( j=0; j<10; j++)
result[i][j] = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
cout<<endl;
}
cout<<"Multiplication after matrix: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
cout<<endl;;
}
}
int main()
{
int i, j,s;
cout<<"Enter the sides of polygon :\n";
cin>>s;
cout<<"Enter the coordinates of polygon :\n";
cout<<"Enter x coordinates ";
for(i=0; i<s; i++)
cin>>mat[0][i];
cout<<"Enter y coordinates ";
for(i=0; i<s; i++)
cin>>mat[1][i];
cout<<"\n\n";
for(i=0; i<s; i++)
mat[2][i] = 1;
cout<<"MAt: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
multiply_matrix(reflect, mat);
cout<<"End"<<endl;
return 0;
}
I have attached a sample output image for reference:
Output of code
I am a newbie and have tried various things but I am just not able to figure my mistake. Thanks in advance for your help.
You have two s in your code. Globally:
int i , j,k,s;
And in main:
int i, j,s;
The one in main you assign a value read from user input, but the global one is 0 always. multiply_matrix uses the global one, hence this loops have zero iterations:
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
and
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}

'' was not declared in this scope

I wrote this program for the college. The user must input 2 numbers (n,m) and the program must make an array with the size [n,m]. Then the user will fill the array and the program must find the maximum number of the columns and print the minimum of them. Then it must find the minimum number of the lines and print the maximum. The compiler says 'n' was not declared in this scope and i don't know why.
Can you help me?
Thank's in advance.
#include <iostream>
using namespace std;
void max (int pinakas[n,m]) {
int i,j,max,pinm[m],min;
for (j=0; j<m; j++){
max=pinakas[0,j];
for (i=0; i<n; i++)
if (pinakas[i,j]>max)
max=pinakas[i,j];
pinm[j]=max;
}
min=pinm[0];
for (j=0; j<m; j++)
if (pinm[j]<min)
min=pinm[j];
cout << min;
}
void min (int pinakas[n,m]) {
int i,j,max,pinm[n],min;
for (i=0; i<n; i++){
min=pinakas[i,0];
for (j=0; j<m; j++)
if (pinakas[i,j]<min)
min=pinakas[i,j];
pinm[i]=min;
}
max=pinm[0];
for (i=0; i<n; i++)
if (pinm[i]>max)
max=pinm[i];
cout << max;
}
int main (){
int n,m,i,j;
cin >> n >> m;
int pin[n,m];
for (i=0; i<n; i++)
for (j=0; j<m; j++)
cin >> pin[i,j];
max(pin[n,m]);
min(pin[n,m]);
return 0;
}
You have tones of errors on your code. I suggest you to give a look at parameters in functions and variable declarations.

How to get calculation in float when we divide the two array's integers in C++;

I need to solve a problem below in which I need to print final result as a float,
but this code gives the output as an int value.
#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0; i<T; i++)
{
int N;
cin>>N;
int A[N],B[N];
float K=0;
for(int j=0; j<N; j++)
cin>>A[i];
for(int j=0; j<N; j++)
cin>>B[i];
for(int j=0; j<N; j++)
K=(B[i]/A[i]) + K ;
cout<<K<<endl;
}
return 0;
}
Input:
1
2
4 2
10 6
Output: 6
Desired output: 5.500000
With some C++14 trickery you can simplify your computation to:
float K = std::inner_product(begin(B), end(B), begin(A), 0.0f, std::plus<float>{}, std::divides<float>{});
Demo
Also note that dynamic arrays are a GCC extension, so the following code is not portable:
int N;
cin>>N;
int A[N],B[N];
We can use a simple vector instead (I did it for the code snippet above):
vector<int> A, B;
int tmp;
for(int j=0; j<N; j++){
cin >> tmp;
A.push_back(tmp);
}
for(int j=0; j<N; j++){
cin >> tmp;
B.push_back(tmp);
}
Explanation:
std::inner_product is primarily meant for... well, computing the inner product (sum of pairwise multiplications) on two collections. However, it parameterizes both the "sum" aspect and the "product" aspect, so we can sub out the "product" for a "division". The C++14 std::divides and std::plus function objects make it easier for us to specify the operations we want (else we could write a lambda for C++11).
You need type cast one of the values of the calculation to be a (float) for the result to be a float value:
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
for(int i = 0; i < T; i++)
{
int N;
cin >> N;
int A[N], B[N];
float K = 0;
for(int j = 0; j < N; j++)
cin >> A[j];
for(int j = 0; j < N; j++)
cin >> B[j];
for(int j = 0; j < N; j++)
K += (float)B[j] / A[j];
cout << K << endl;
}
return 0;
}

How to send 2 dimensional Matrix with unknown size to a function

I'm trying to figure out how to pass a 2 dimensional Matrix to a function
//function to print Matrix
void refl(int n, int board[][])
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
int board[n][n]; //creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
refl(n , board);
}
return 0;
}
It says that "n" and "board" isn't declared in the function while they are .
Try to use C++ std::vector or good old C malloc+free.
C++
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void reflxx(int n, vector<vector<int>>& board)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
vector<vector<int>> board(n, vector<int>(n));
//creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
}
reflxx(n , board);
return 0;
}
See http://www.cplusplus.com/reference/vector/vector/resize/ for more example.

Bad Access C++ Error

Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.
It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.