C++ get length from two dimensional dynamic array - c++

I have got stuck by the C++ two dimensional dynamic array. I want to get the array length. Here is the code:
#include <iostream>
using namespace std;
int dosomestuff(char **dict);
int main(){
int x, y;
char **dict;
cin>>x>>y; // here to input the 'x'
dict = new char *[x];
for(i = 0; i < x; i++){
dict[i] = new char[y];
for(j = 0; j < y; j++){
cin>>dict[i][j];
}
}
dosomestuff(dict);
}
int dosomestuff(char **dict){
int x, y;
x = sizeof(*dict); //8 not equal to the 'x'
//run in mac_64 I think this is the pointer's length
y = strlen(dict[0]); //this equal to the 'y' in function main
cout<<x<<" "<<y<<endl;
return 0;
}
What I want is in function dosomestuff to get the x equal the 'x' in function main.
How can I to get it? Can anybody help me ~? Thx a lot.

sizeof(*dict) only gives you the sizeof(char*), which is not what you are hoping for.
There is no way of knowing the value of x from dict in dosomestuff. If you want to use char** for dict, your best option is to pass x and y to dosomestuff.
int dosomestuff(char **dict, int x, int y);
Since you are using C++, you can use:
std::vector<std::string> dict;
Then you would have all the information you need in dosomestuff if you pass that dict to it.

Related

Invoking function with paremeters, one of which is an array of structures in C++?

I have a program where I have a function that sorts elements of an array of structures by their key field. However, when I invoke the function Insertion(a[],7) - I pass the array and its size, the compiler gives an error expected primary expression before ']' token. I would like to ask what am I doing wrong?
#include <iostream>
using namespace std;
struct CElem
{
int key;
};
CElem a[7];
void Insertion(CElem m[],int n)
{
CElem x;
int i;
int j;
for (i = 0; i < n; i++)
{
x = m[i];
j = i-1;
while (j >= 0 && x.key < m[j].key)
m[j+1] = m[j--];
m[j+1] = x;
}
}
int main()
{
a[0].key=32;
a[1].key=45;
a[2].key=128;
a[3].key=4;
a[4].key=-9;
a[5].key=77;
a[6].key=-7;
Insertion(a[],7);
return 0;
}
you only need to pass the pointer to the start of the array:
Insertion(a, 7);
Your parameter m of the method Insertion is of type CElem*. The variable a is of type CElem* too so you are supposed to give the method just a, like Insertion(a,7);.

increasing array index in a function

i'm willing to write a code to create an array with a changeable index(meaning having a static array in a function and adding values to it and then getting out of the loop and coming back again and adding another value to the end of it)
but my code doesn't compile:
#include <iostream>
#include <conio.h>
using namespace std;
void arrarr(int);
int main()
{
for (int i = 1; i < 5; i++)
{
arrarr(i);
}
_getch();
return 0;
}
void arrarr(int y)
{
static int x[y];
x[y] = 5;
cout << x[y];
}
Variables can not have variable size. You have to define explicitly the size of the array x, for example: static int x[5].
Also, arrays are zero-indexed, meaning the first element starts at 0. So your loop condition should be for (int i = 0; i < 5; i++)
C++ doesn't support variable length array, you need to define constant size for the static array x. You can do something like:
void arrarr(int y)
{
static int x[SOME_CONSTANT_SIZE]; //SOME_CONSTANT_SIZE known at compile time
x[y] = 5; //y < SOME_CONSTANT_SIZE
cout << x[y];
}
And as #Bruno pointed out, array indices start from 0 to (size-1)
If you are looking to dynamically increase the size of your array, you cannot do that. Use a vector instead. See here: http://www.cplusplus.com/reference/vector/vector/resize/
Your code doesn't compile because of this:
static int x[y];
The compiler doesn't know what x is, e.g.
int [0]
int [1]
int [2]
So you can do
void arrarr(int y) {
static int x[10];
x[y] = 5;
cout << x[y];
}
but what you were doing won't work.
If you share what the expected output is, we might be able to help you more.

How to print out 2d arrays with function in C++?

I am trying to make a function that prints out two dimensional arrays.
I did one that prints out 1d arrays.
#include <iostream>
using namespace std;
void printArray (int theArray[],int sizeOfArray);
int main ()
{
int array1[3] = {1,3,7};
int array2[5] = {123,5,23,2,324};
printArray(array1, 3);
printArray(array2, 5);
}
void printArray (int theArray[],int sizeOfArray){
for (int x=0; x<sizeOfArray; x++) {
cout<<theArray[x] <<" ";
}
cout<<endl;
}
I wrote these codes for printing out 2d arrays but I failed.
#include <iostream>
using namespace std;
void printArray (int theArray[][],int sizeOfRow, int sizeOfCol);
int main ()
{
int array[2][3] = {{1,3,7},{5,3,2}};
printArray(array, 2,3);
}
void printArray (int theArray[][],int sizeOfRow, int sizeOfCol){
for (int x=0; x<sizeOfRow; x++)
for (int y=0; y<sizeOfCol; y++) {
cout<<theArray[x][y] <<" ";
}
cout<<endl;
}
My compiler says array has incomplete element type 'int[]'.
What are the right codes for printing out 2d arrays?
Since array size has to be known during compile time, you can use templates to provide flexibility to the function.
template< typename T, size_t N, size_t M >
void printArray( T(&theArray)[N][M] ) {
for ( int x = 0; x < N; x ++ ) {
for ( int y = 0; y < M; y++ ) {
cout << theArray[x][y] << " ";
}
}
}
printArray( array );
this is much nicer since there is no need to pass the dimension of array anywhere, also work for 3D, 4D array by adding extra parameter to the template.
For a multi-dimensional array you need to declare the size of the array in your function declaration.
So your method needs to look like:
void printArray (int theArray[][3],int sizeOfRow, int sizeOfCol)
Then it should compile and work.
A tip for printing out two dimensional arrays -- it is easier to use the variables r and c (i.e. for row and columns) to visualize how the array is being printed out.
As noted above, the column size is REQUIRED by the language when passing 2d arrays.
The function header should look as follows:
void printArray (int theArray[][3],int sizeOfRow, int sizeOfCol)

First time using arrays, getting an error that 'x was not declared in this scope' however I did declare it?

My compiler keeps saying that 'small' and 'x' were not declared in this scope, how do I fix my array so that they are accurately displayed? overall the code is supposed to find the smallest positive nonzero value stored in the array.
#include <iostream>
#include <string>
using namespace std;
int findthesmall( int small[x], int y)
{
for(int i=0; i< y; i++){
for(int j=0; j< y; j++){
int temp = small[i];
if( small[i] > small[j] )
small[i] = small[j];
small[j] = temp;
}
}
return small[0];
}
int main(){
return 0;
}
I think you need:
int findthesmall( int* small, int y) {
Try this:
int findthesmall( int small[], int y) {
for(int i=0; i< y; i++){
for(int j=0; j< y; j++){
int temp = small[i];
if( small[i] > small[j] )
small[i] = small[j];
small[j] = temp;
}
}
return small[0];
}
int main(){
return 0;
}
int small[x]
This is illegal for 2 reasons.
Like your compiler says, X is undefined
Size of the array cannot be set to the value of a non compile time constant.
To fix this you can do what #ajon suggested( pass array as pointer + length), it is historically the way to pass arrays.
There are other better ways in C++ though.
You can consider using std::array or std::vector. Both of them can be passed as you would any other variable, know their own size, and can be accessed like a normal array
Or you could use template code to capture the size of the array automatically.
template<int len>
int findthesmall(int (&small)[len]){
The 2nd option maybe a little convoluted and more complex than other options, especially now that you have got your answer, I'm just including it here for completeness.
Apart from other answers, there is also a bug in the logic. If your function is just to find the smallest element as function name indicated, one for loop should be enough.
Sample code presented below:
int findthesmall( int small[], int y)
{
int temp = small[0];
for(int i=1; i< y; i++)
{
if( temp > small[i] )
temp = small[i];
}
return temp;
}
Or you could use std::min_element algorithm as well
std::cout << *std::min_element(small, small+y) << std::endl;

Recognizing an integer as a float in 2 dimensional array subscript

Here's the code that I'm currently using
template <class T>
float Knapsack<T>::knapTable()
{
const int MAXSIZE = 40000;
int temps = nObjects - 1;
float** memoize = new float*[MAXSIZE];
for(int y = 0; y < weight[nObjects]; y++)
memoize[nObjects][y] = 0;
for(int y = weight[nObjects]; y < knapCap; y++)
memoize[nObjects][y] = price[y];
for(int i = temps; i >= 1; i--)
{
for(int y = weight[i]; y < knapCap; y++)
memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
}
return memoize[1][nObjects];
}
For some reason I keep getting the error: knapsack.hpp:68:64: error: invalid types ‘float*[float]’ for array subscript.
That's this line: float** memoize = new float*[MAXSIZE];
For some reason the compiler seems to be recognizing MAXSIZE as a float, it's a const int.
Is there a way I can fix this?
Edited for more code
header file
#ifndef KNAPSACK_H
#define KNAPSACK_H
#include <stdexcept>
#include <assert.h>
#include <iostream>
#include <limits.h>
using namespace std;
template <class T>
class Knapsack
{
private:
float knapPrice;
int knapCap, nObjects;
float weight[40000];
float price[40000];
public:
Knapsack(): knapPrice(0), knapCap(0), nObjects(0) {}
~Knapsack() {knapPrice = 0; knapCap = 0;}
float knapFull (int position, int currentCap);
float knapTable ();
float greedyKnap (int currentCap);
float max(float noAdd,float addOb);
void printPrice();
//valueized and valued are modified versions of mergeSort and merge
//designed to sort two arrays by a fraction of the two.
void valueize(int ini, int last);
void valued(int ini, int middle, int last);
void fillWandP();
void setNObjects(int n);
void setKnapCap(int boom);
};
#include "knapsack.hpp"
#endif
Main function //Though I don't think this would affect it
#include "sortClass.h"
#include "knapsack.h"
#include
#include
#include
#include
using namespace std;
//mergeSort main;
int main()
{
Knapsack<float> a;
float sacked = 0;
int nO = 18;
int cap = 700;
a.setNObjects(nO);
a.setKnapCap(cap);
a.fillWandP();
for(int b = 0; b <3800000; b++)//for getting good times
sacked = b;
int startAll = clock()*1000000;
sacked = a.knapFull(1, cap);
int knapped = clock()*1000000;
int boom = a.knapTable();
int tabled = clock()*1000000;
a.valueize(1, cap);
int andDone = a.greedyKnap(cap);
int greedified = clock()*1000000;
cout<<startAll<<endl;
greedified = greedified - tabled;
tabled = tabled - knapped;
knapped = knapped - startAll;
cout<<"Recursion profit:"<<sacked<<" Time: "<<knapped<<endl;
cout<<"Memoization profit:"<<boom<<" Time: "<<tabled<<endl;
cout<<"Greedy profit: "<<andDone<<" Time: "<<greedified<<endl;
return 0;
}
weight is declared as float weight[40000] in class Knapsack.
You then use an element of weight as an index into memoize in the knaptable() function:
memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
// ^^^^^^^^^
And for the record, that's the line that the error is produced for by g++ 4.6.1; it doesn't point to the line where memoize is declared.
Not necessarily related, but you're not using your arrays/pointers correctly. You create your first level of pointers when you call float** memoize = new float*[MAXSIZE] but you then just have an array of pointers, not a double array. You need to initialize each of memoize[i] as an array as well.
That being said, it doesn't look like you should be allocating memory for your memoize array anyway. Just declare it as
float memoize[SIZE][SIZE];
That way, you won't have to worry about memory cleanup or anything, and it makes a lot more sense.
for(int i = temps; i >= 1; i--)
{
for(int y = weight[i]; y < knapCap; y++)
memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
}
y-weight[i] is a float. This is your problem.
Once you fix this you will discover that you still have an issue, you're allocating an array of pointers but you also need to allocate the second dimension for each of those pointers before you can use that array.
Something along the lines of:
float** memoize = new float*[MAXSIZE];
for(size_t i = 0; i < MAXSIZE; ++i)
{
memoize[i] = new float[MAXSIZE];
}
i think maybe you just need to allocate memory for the second pointer ,something like
float** memoize = new float*[MAXSIZE];
memoize=(float**)malloc(sizeof(float*)*MAXSIZE);
for(int i=0;i<MAXSIZE;i++)
{
memoize[i]=(float*)malloc(sizeof(float)*MAXSIZE);
}