I have no idea what's going on how to imagine that, one more thing like in 3rd for loop there's a condition that k<j but its upper loop j is set to 0 and i is also 0 then as I think k=0 if this is right than 0<0 how's that can be valid????
void printing_subarrays(int *arr,int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int k=i; k<j; k++){
cout<<arr[k]<<", ";
}cout<<endl;
}cout<<endl;
}
}
I don't think it makes much sense for us to explain what's happening, you need to see it for yourself.
Therefore I've added some output lines, which will show you how the values of the variables i, j and k evolve through the loops:
for(int i=0; i<n; i++){
cout<<"i=["<<i<<"]"<<endl;
for(int j=0; j<n; j++){
cout<<"i=["<<i<<"], j=["<<j<<"]"<<endl;
for(int k=i; k<j; k++){
cout<<"i=["<<i<<"], j=["<<j<<"], k=["<<k<<"]"<<endl;
cout<<arr[k]<<", ";
}
cout<<endl;
}
cout<<endl;
}
Did you try maybe running it? Then it should be apparent...
#include <iostream>
#include <vector>
void printing_subarrays(int *arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = i; k < j; k++) {
std::cout << arr[k] << ", ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
}
int main() {
int n = 10;
std::vector<int> vec(n);
for (size_t i = 0; i < n; ++i) {
vec[i] = i;
}
printing_subarrays(vec.data(), n);
return 0;
}
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)
}
I am using a classifier algorithm for a digital voice signal processing project. This algorithm was developed to receive all the audio signals in a single vector to do the processing, but I'm having problems, because the number of files I'm working on is very large and is generating the error "terminate called after throwing an instance of ' std :: bad_alloc 'what (): std :: bad_alloc ". I would like to know if it is possible to make any changes to the code that reads the files and stores them in the vector more efficiently, without exceeding the available memory space.
Code for reading the files:
string filename;
filename="C:\\Users\\marcu\\Desktop\\TCC\\Arquivos_10780\\Arquivos_DFT_TXT_512\\PA_D_00";
std::vector<double> c;
for(int j=1; j<=5400; j++)
{
stringstream ss;
ss << filename << setw(5) << setfill('0') << j << "_bonafide_DFT.txt";
std::ifstream f;
f.open(ss.str().c_str());
if (f.is_open())
{
double num;
while (f >> num)
c.push_back(num);
f.close();
}
else
{
f.close();
continue;
}
}
for(int j=5401; j<=29700; j++)
{
stringstream ss;
ss << filename << setw(5) << setfill('0') << j << "_spoof_DFT.txt";
std::ifstream f;
f.open(ss.str().c_str());
if (f.is_open())
{
double num;
while (f >> num)
c.push_back(num);
f.close();
}
else
{
f.close();
continue;
}
}
Full code:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<iomanip>
#include<sstream>
using namespace std;
double mean_similarities(double**,int,int);//vectors, number of vectors, their dimension
int main()
{
const int number_of_classes=2;
int number_of_feature_vectors_in_class[number_of_classes];
number_of_feature_vectors_in_class[0]=2700;
number_of_feature_vectors_in_class[1]=8080;
const int dimension_of_each_feature_vector=512;
////////////////////////////////////////////////////////////////////////////////////////////
/*
Example: 3 classes and 4 vectors of dimension 2 in each class
{{0.90,0.12},{0.88,0.14},{0.88,0.13},{0.89,0.11}} //0.88---0.90 ; 0.11---0.14
{{0.55,0.53},{0.53,0.55},{0.54,0.54},{0.56,0.54}} //0.53---0.56 ; 0.53---0.55
{{0.10,0.88},{0.11,0.86},{0.12,0.87},{0.11,0.88}} //0.10---0.12 ; 0.86---0.88
double c[]={
0.90,0.12,0.88,0.14,0.88,0.13,0.89,0.11,
0.55,0.53,0.53,0.55,0.54,0.54,0.56,0.54,
0.10,0.88,0.11,0.86,0.12,0.87,0.11,0.88
//all vectors in class C_1, followed by all vectors in C_2, ...., followed by all in C_n
};
*/
////////////////////////////////////////////////////////////////////////////////////////////
string filename;
filename="C:\\Users\\marcu\\Desktop\\TCC\\Arquivos_10780\\Arquivos_DFT_TXT_512\\PA_D_00";
std::vector<double> c;
for(int j=1; j<=5400; j++)
{
stringstream ss;
ss << filename << setw(5) << setfill('0') << j << "_bonafide_DFT.txt";
std::ifstream f;
f.open(ss.str().c_str());
if (f.is_open())
{
double num;
while (f >> num)
c.push_back(num);
f.close();
}
else
{
f.close();
continue;
}
}
for(int j=5401; j<=29700; j++)
{
stringstream ss;
ss << filename << setw(5) << setfill('0') << j << "_spoof_DFT.txt";
std::ifstream f;
f.open(ss.str().c_str());
if (f.is_open())
{
double num;
while (f >> num)
c.push_back(num);
f.close();
}
else
{
f.close();
continue;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
//edit whatever you need, according to the feature vectors of your problem, ABOVE this line.
//Do NOT change anything BELOW this line !!!!!
////////////////////////////////////////////////////////////////////////////////////////////
double*** C=new double**[number_of_classes];
for(int i=0; i<number_of_classes; i++)
C[i]=new double*[number_of_feature_vectors_in_class[i]];
for(int i=0; i<number_of_classes; i++)
for(int j=0; j<number_of_feature_vectors_in_class[i]; j++)
C[i][j]=new double[dimension_of_each_feature_vector];
int l=0;
for(int i=0; i<number_of_classes; i++)
for(int j=0; j<number_of_feature_vectors_in_class[i]; j++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
{
C[i][j][k]=c[l];
l++;
}
//Debug info only
//for(int i=0;i<number_of_classes;i++)
// for(int j=0;j<number_of_feature_vectors_in_class[i];j++)
// for(int k=0;k<dimension_of_each_feature_vector;k++)
// printf("\nclass %d vector %d element %d is %.3f",i,j,k,C[i][j][k]);
//getchar();
double Y[number_of_classes];
for(int i=0; i<number_of_classes; i++)
Y[i]=mean_similarities(C[i],number_of_feature_vectors_in_class[i],dimension_of_each_feature_vector);
double alpha=Y[0];
for(int i=1; i<number_of_classes; i++)
if(Y[i]<alpha)
alpha=Y[i];
printf("\nALPHA: %.3f",alpha);
double** smallest_range_vector_for_class=new double*[number_of_classes];
for(int i=0; i<number_of_classes; i++)
smallest_range_vector_for_class[i]=new double[dimension_of_each_feature_vector];
for(int i=0; i<number_of_classes; i++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
smallest_range_vector_for_class[i][k]=C[i][0][k];
for(int i=0; i<number_of_classes; i++)
for(int j=1; j<number_of_feature_vectors_in_class[i]; j++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
if(C[i][j][k]<smallest_range_vector_for_class[i][k])
smallest_range_vector_for_class[i][k]=C[i][j][k];
//Debug info only
//for(int i=0;i<number_of_classes;i++)
// for(int k=0;k<dimension_of_each_feature_vector;k++)
// printf("\nclass %d smallest component %d is %.3f",i,k,smallest_range_vector_for_class[i][k]);
double** largest_range_vector_for_class=new double*[number_of_classes];
for(int i=0; i<number_of_classes; i++)
largest_range_vector_for_class[i]=new double[dimension_of_each_feature_vector];
for(int i=0; i<number_of_classes; i++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
largest_range_vector_for_class[i][k]=C[i][0][k];
for(int i=0; i<number_of_classes; i++)
for(int j=1; j<number_of_feature_vectors_in_class[i]; j++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
if(C[i][j][k]>largest_range_vector_for_class[i][k])
largest_range_vector_for_class[i][k]=C[i][j][k];
//Debug info only
//for(int i=0;i<number_of_classes;i++)
// for(int k=0;k<dimension_of_each_feature_vector;k++)
// printf("\nclass %d largest component %d is %.3f",i,k,largest_range_vector_for_class[i][k]);
int R=0;
int F=0;
for(int ia=0; ia<number_of_classes; ia++)
for(int ib=0; ib<number_of_classes; ib++)
for(int j=0; j<number_of_feature_vectors_in_class[ib]; j++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
{
if(ib!=ia)
{
if((C[ib][j][k]>smallest_range_vector_for_class[ia][k])&&(C[ib][j][k]<largest_range_vector_for_class[ia][k]))
R++;
F++;
}
}
double beta=((double)(R))/((double)(F));
printf("\nBETA: %.3f",beta);
printf("\nP=(G1,G2)=(%.3f,%.3f)",alpha-beta,alpha+beta-1);
printf("\nDistance from P to (1,0): %.3f",sqrt(pow((alpha-beta)-1,2)+pow(alpha+beta-1,2)));
printf("\n\n");
}
/////////////////////////////////////////////
////////////////////////////////////////////
double mean_similarities(double** v,int n, int t)
{
double largest;
double smallest;
double* s=new double[t];
for(int i=0; i<t; i++)
{
smallest=1;
largest=0;
for(int j=0; j<n; j++)
{
if(v[j][i]>largest)
largest=v[j][i];
if(v[j][i]<smallest)
smallest=v[j][i];
}
s[i]=1-(largest-smallest);
}
double m=0;
for(int i=0; i<t; i++)
m+=s[i];
m/=((double)(t));
return(m);
}
PS: to find the best classifier result, I need to change the size of the file size (amount of information for each file) to increasingly larger values. At first with 512 points, but I double this value with each execution until I reach 8192, but when I try with 16384 the code crashes. I am working with 10780 files where each one has the same dimension and I increase it as I check the result.
First of all, you have a lot of problems below:
double** largest_range_vector_for_class=new double*[number_of_classes];
for(int i=0; i<number_of_classes; i++)
largest_range_vector_for_class[i]=new double[dimension_of_each_feature_vector];
for(int i=0; i<number_of_classes; i++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
largest_range_vector_for_class[i][k]=C[i][0][k];
for(int i=0; i<number_of_classes; i++)
for(int j=1; j<number_of_feature_vectors_in_class[i]; j++)
for(int k=0; k<dimension_of_each_feature_vector; k++)
if(C[i][j][k]>largest_range_vector_for_class[i][k])
largest_range_vector_for_class[i][k]=C[i][j][k];
I can't even read that, but I do get the gist of it, basically you created a dynamic array, a 3D dynamic array, but nowhere in your program did you free the memory that was used by the 3D array, it looks very leaky, I cannot delete for you because I have trouble reading that monstersity....
i am trying to add two 2D arrays in C++ with my following code by i am getting output as this
333 333, but i want out as 2 rows
{
int a[2][3], b[2][3], i , j;
cout<<"First Matrix"<<endl;
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>a[i] [j];
}
}
cout<<"Second Matrix"<<endl;
for(int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>b[i][j];
}
}
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
}
cout<<" ";
}
cout<<endl;
_getch();
}
Last for loop is wrong. You have to move cout's.
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
cout<<" ";
}
cout<<endl;
}
Also your variables i and j are unused because you are declaring new ones in for loops with int i=0; and int j=0;.
How about changing the line that prints spaces to print a newline?
cout<<" ";
becomes
cout<<"\n";
You don't put any newlines in your code, so of course it won't print out on a new line. replace cout<<" "; with cout<<std::endl; and you should get each row on a new line.
Replace the last piece of code by this:
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j] << ' ';
}
cout<< "\n";
}
cout << "\n";
Is it possible to use the operator [] in case of 2d vectors? For example i got the following code:
vector<vector<string>> data;
ifstream myReadFile;
myReadFile.open("stoixeia_astheni.txt");
while (!myReadFile.eof()) {
for(int i=0; i<1; i++){
for (int j=0; j<4; j++){
myReadFile >> data[i][j];
}
}
}
I got message out of range. I have a file with 5 lines and 4 columns.
Your vector data is empty, its size() is 0. You have to resize it first or add new elements by using push_back():
while (!myReadFile.eof()) {
for(int i = 0; i < 1; i++){
vector<string> tmpVec;
string tmpString
for (int j = 0; j < 4; j++){
myReadFile >> tmpString;
tmpVec.push_back(tmpString);
}
data.push_bac(tmpVec);
}
}
You could also set the size right at the declaration of data:
vector<vector<string>> data(5,vector<string>(4));
ifstream myReadFile;
myReadFile.open("stoixeia_astheni.txt");
while (!myReadFile.eof()) {
for(int i=0; i < 5; i++){
for (int j=0; j<4; j++){
myReadFile >> data[i][j];
}
}
}