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.
Related
Doing some simple exercies with C++ and I'm stuck...
When my sum[t] array is declared as integer or float it sometimes outputs at the end some crazy values like for example 4239023 or -3.17802e+30 (despite the fact that I add only small numbers from range <-100; 300>). When I change it for double it works correctly. Why int and float don't work here?
#include <iostream>
using namespace std;
int main()
{
int t=0;
int n=0;
int x=0;
cout<<"Amount of sums: ";
cin>>t;
int sum[t];
for (int i=0; i<t; i++)
{
cout<<"Sum no. "<<i+1<<". How many numbers you wish to add: ";
cin>>n;
for (int j=0; j<n; j++)
{
cout<<"Insert number "<<j+1<<" : ";
cin>>x;
sum[i]+=x;
}
}
for (int i=0; i<t; i++)
{
cout<<sum[i]<<endl;
}
return 0;
}
You should initialize your array of sums to zeroes after you receive the value of t. You could do it like this:
for (int i=0; i<t; i++)
{
sum[i] = 0;
}
I was practicing some questions from hacker rank and got stuck in this question called sparse-arrays. (https://www.hackerrank.com/challenges/sparse-arrays/problem) I am a beginner and i am not able to find out the error in my code. Please help. Thank you
I think I am not comparing the strings correctly. I tried using compare function but still it did not work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin>>n;
string strings[n];
for(int i=0; i<n; i++){
cin>>strings[i];
}
int m;
cin>>m;
string queries[m];
for(int i=0; i<m; i++){
cin>>queries[i];
}
//comparing
for(int i=0; i<m; i++)
{
int count=0;
for(int j=0; j<n; j++)
{
if(queries[i]==strings[j])
count++;
}
cout<<count<<endl;
}
}
The output should be the number of times the strings in (query) that have appeared in the (strings) but my program is getting terminated please help.
for(int j=0; i<n; j++)
should be
for(int j=0; j<n; j++)
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;
}
EDIT: I'm fairly new to c++. Began working with this language two weeks ago.
Sorry if this have been asked before, but i have searched everywhere on the web on how to sum individual rows in a 2D array and not found the answer that i was looking for.
i need to display the sum of each individual row in a[m][n], but for some reason this only works when my array is 2x2, but if it's 3x3 or bigger, then i get the following output in the terminal:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
This is what i have so far
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
You're not initializing these values so they're free to be whatever cruft is on the stack at the time. You need to initialize them.
int avg[m];
for (int i = 0; i < m; ++i) {
avg[i] = 0;
for (int j = 0; j < n; ++j) {
avg[i] += a[i][j];
}
}
int a[m][n]; is not allowed in Standard C++. The dimensions of C-style arrays must be known at compile-time. A program using this code could do literally anything.
You could replace this line with:
vector<vector<int>> a(m, vector<int>(n));
which seems like a mouthful at first, but you will find it makes your problem go away.
Another bonus of this approach is that you can then use range-based loops:
for(int x : avg)
cout << x << endl;
which reduces the chance of making an error by using the wrong letter in the loop condition.
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.