i have this for my requirement assignment:
Write a search function int get_index(const float astm[?][?],
const int size) to find the array index from a given size.
can someone give me some hint on how to di it pleass?
i have this for my requirement assignment:
Write a search function int get_index(const float astm[?][?],
const int size) to find the array index from a given size.
can someone give me some hint on how to di it pleass?
Show the code you have so far ... and where you think you get stuck.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
const int COLa = 4;
const int ROWa = 9;
const int REBAR = 2;
void discard_line(ifstream &in);
void print_ASTM_table(const float astm[ROWa][COLa]);
int get_index(const float astm[ROWa][COLa],const int size);
int main()
float astm[ROWa][COLa] = {{0}}; // declare 2D array for astm table 1.1
int total;
int choice; // declare choice enter by the user
char c;
ifstream input;
ifstream infile;
cout<<"Enter the number of your choice : ";
cin>>choice; // the first choice
cout<<endl;
if (choice == 1) // if the choice is 1
{
input.open("F:\\astm.txt",ios::in);
discard_line(input); //discard the header
// check if file not found
if(input.fail())
{
cerr<<"File not found!"<<endl;
system("PAUSE");
exit(1);
}
while(!input.eof())
{
// store the table ASTM standards for rebars in a two dimensional array
for(int row =0;row<ROWa;row++)
{
for(int col = 0;col<COLa;col++)
{
input>>astm[row][col];
}
cout<<endl;
input.get(c);
}
// print ASTM table for rebars in a nice format as shown in Table 1.1.
print_ASTM_table(astm);
}
}
else if (choice == 2) // If the choice is 2
{
get_index(astm,ROWa);
cout<<"Program submenu from reading rebars from a file: "<<endl;
cout<<"(1) Print the total weight for each rebars used."<<endl;
cout<<"(2) print memory address of weight and diameter values for a rebars of size 3."<<endl;
cout<<"(3) Exit "<<endl;
cout<<"Enter the number of your choice : ";
cin>>choice;
if(choice == 1)
{
infile.open("D:\\rebars.txt",ios::in);
discard_line(infile);
// check if file fail to read
if(infile.fail())
{
cerr<<"File not found!"<<endl;
exit(1);
}
while(!infile.eof())
{
for(int row = 0;row<REBAR;row++)
{
for(int col = 0;col<REBAR;col++)
{
infile>>astm[row][col];
cout<<astm[row][col]<<" ";
}
cout<<endl;
}
}
}
}
else{
system("Pause");
return 0;
}
system("PAUSE");
return 0; // End of the program
}
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
void print_ASTM_table(const float astm[ROWa][COLa])
{
cout<<" Size "<<" "<<"Lenght "<<" "<<"Weight "<<" "<<"diameter(in) ";
cout<<"\n"<<endl;
// 2D array
for(int row =0;row<ROWa;row++)
{
for(int col = 0;col<COLa;col++)
{
cout<<setw(7)<<astm[row][col]<<" ";
}
cout<<endl;
}
}
int get_index(const float astm[ROWa][COLa],const int size) // Search function here
{
if(astm[ROWa][COLa] == size && COLa == 1)
return ROWa ;
}
From your code, I'm assuming the matrix will be filled completely, so ROWa is a valid limit on number of rows to search.
If the size you search for can be in any position in a row, the best you're going to be able to do is return the row's index in which it's found. If it is in a specific column of any row, that's all you'd need.
The simplest search goes something like (1D array example):
int sch( int d[], int limit, int tgt )
{
int i;
for( i = 0; i < limit; i++ )
if( d[i] == tgt )
return i;
return -1; //tgt not found, invalid index
}
Thanks i write again my code like this and change the calling function below as
get_index(astm,size);
int get_index(const float astm[COLa][ROWa],const int size) // Search function here
{
int index;
int siz;
// if statement of the search function
if (astm[COLa][ROWa]==size && COLa==1)
{
index = ROWa;
siz = COLa;
}
else if(index == 0)
{
siz = 2;
}
else if(index == 1)
{
siz = 3;
}
else if(index == 2)
{
siz = 4;
}
else if (index == 3)
{
siz = 5;
}
else if(index == 4)
{
siz = 7;
}
else if(index == 5)
{
siz = 8;
}
else if(index == 6)
{
siz = 9;
}
else if(index == 7)
{
siz = 12;
}
else if(index == 8)
{
siz = 14;
}
else
{
cout<<"not index found!"<<endl;
}
return index;
}
but the program keep saying [Linker error] undefined reference to `get_index(float const (*) [4], int)'
ld returned 1 exit status
what is wrong with my code?
You seem to be making an easy thing hard ...
See this example of reading a 2D matrix from file, printing it back and then searching it to find a point ...
// myMatrixSearch.cpp //
/*
2 5
1 2 3 4 5
6 7 8 9 0
with the leading 2, indicating the number of rows
with the next 5, indicating the number of col's
you could read this into a matrix like this (using the STL vector container) ...
*/
const char* FNAME = "matrixData.txt";
#include <iostream>
#include <iomanip> // re. setw( )
#include <fstream>
#include <vector>
using namespace std;
ostream& operator << ( ostream& os, const vector< vector < int > >& v )
{
for( size_t i = 0; i < v.size(); ++ i )
{
for( size_t j = 0; j < v[i].size(); ++ j )
{
os << ' ' << setw(8) << v[i][j];
}
os << endl;
}
return os;
}
struct Point
{
int x, y;
Point() : x(-1), y(-1) {} // NOT found value
Point( int a, int b ) : x(a), y(b) {}
void print() const
{
cout << x << ", " << y;
}
} ;
Point find( const vector< vector < int > >& v, int val )
{
for( size_t i = 0; i < v.size(); ++ i )
{
for( size_t j = 0; j < v[i].size(); ++ j )
{
if( val == v[i][j] ) return Point( i, j );
}
}
// else ...
return Point(); // to indicate NOT found ...
}
int main()
{
ifstream fin( FNAME );
if( fin )
{
size_t rows, cols;
fin >> rows >> cols;
vector< vector< int > > mat( rows);
for( size_t i = 0; i < rows; ++ i )
{
mat[i].resize( cols );
for( size_t j = 0; j < cols; ++ j )
fin >> mat[i][j];
}
fin.close();
cout << "Showing back the matrix ... \n" << mat << endl;
// find 9 ...
Point p = find( mat, 9 );
if( p.x != -1 )
{
cout << 9 << " was found at row_idex, col_index = ";
p.print();
}
}
else
cout << "\nThere was aproblem opening file "
<< FNAME << endl;
}
oh ok thanks very much....
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.