phorce 131 Posting Whiz in Training Featured Poster

Your code will compile if some changes are made in line 5, 13, 19 as shown below

#include <iostream>

using namespace std;

int* matrix()
{
        int matrix[10] =
        {
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1
        };

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << matr[i] << "\t";
        }
}

But this is NEVER recommended and there are high chances it might not work. Why? Because the array is defined inside matrix(). So array will reside in the stack area of matrix(). When matrix() goes out of scope (after return to main function) the memory block where the array elements are stored maybe overwritten and hence data loss.

So when you want to return an array from a function either use the static way as suggested by thines01. Or you have to store the array in heap. So even if the function goes out of scope you wont lose your data. It will still be there in heap. But you have to make sure you delete those allocated memory after your use. Here is how you can do it. Note the delete statement at line 22.

#include <iostream>

using namespace std;

int* matrix()
{
        int* matrix = new int[10];
        for (int i = 0; i < 10; i++) matrix[i] = 1;

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << *(matr+i) …
phorce 131 Posting Whiz in Training Featured Poster

Yeahh, I removed the * and it displayed an error : invalid types 'int[int]' for array subscript

phorce 131 Posting Whiz in Training Featured Poster
#include <iostream>

using namespace std;

int matrix()
{
	int matrix[10] = 
	{
		1, 1, 1, 1, 1,	
		1, 1, 1, 1, 1	
	};
	
	return *matrix;
	
}
int main(int argc, char *argv[]) {
	
	int static matr = matrix();	

	for(int i=0; (i < 10); i++)
	{
		cout << matr[i] << "\t";
		
	}
}

error: invalid types 'int[int]' for array subscript

When making it static

phorce 131 Posting Whiz in Training Featured Poster

Thanks :)

My compiler gives this error when removing the *:

Untitled.cpp: In function 'int matrix()':
Untitled.cpp:13: error: invalid conversion from 'int*' to 'int'
Untitled.cpp:7: warning: address of local variable 'matrix' returned
Untitled.cpp: In function 'int main(int, char**)':
Untitled.cpp:22: error: invalid types 'int[int]' for array subscript

phorce 131 Posting Whiz in Training Featured Poster

Haven't really looked at this much (Tired!)

//Game Of Life Assignment
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
	
const int ROWS=12;
const int COLS=30;
char file[ROWS][COLS];

string filename;

cout << "Please enter a filename:";
cin >> filename;

cout << "File: " << filename << endl;
ifstream myfile(filename.c_str());

if(myfile.is_open())
{
	while(myfile.is_open())
	{
		// do something with file
		// do something with file
	}
	
}else{
	
	cout << "Could not open the file";
}
return 0;
}

Also, when assigning a multidimensional array (2d) just assign it as a massive 1D array (that's what it technically is)

Hope this helps anyway :)

phorce 131 Posting Whiz in Training Featured Poster

Hello, basically I want to return an array from a function (So I don't output things in the class - just main)

But I seem to get the error: "invalid types 'int[int]' for array subscript"

Here is the code:

#include <iostream>

using namespace std;

int matrix()
{
	int matrix[10] = 
	{
		1, 1, 1, 1, 1,	
		1, 1, 1, 1, 1	
	};
	
	return *matrix;
	
}
int main(int argc, char *argv[]) {
	
	int matr = matrix();	

	for(int i=0; (i < 10); i++)
	{
		cout << matr[i] << "\t";		
	}
}

Any help would be great :)

phorce 131 Posting Whiz in Training Featured Poster

Text file =).. I would like to be able to change the values in the text file, run the executable and it'll create the matrix.. But, I'm not that far yet!

phorce 131 Posting Whiz in Training Featured Poster

Hey, thank you very much for your reply :)

To answer your question:
Do you know the number of rows and columns before you start? This is really kind of important if you are going from one array to the other.

- Yes I'll know the size of the matrix. (e.g. 10 x 10)

I think I have something of an idea on how to read the matrix in:

I'm going to have a function that reads the contents of the text file into an array. I'm unsure whether or not I will just define the file inside main and then process the file and store it into an array..

OR actually read the contents of the file inside main and then pass the array through a method..

Thoughts? =)

phorce 131 Posting Whiz in Training Featured Poster

GOT IT! :) Thank you haaa!

Now to read in from a text file.. Wish me luck!

phorce 131 Posting Whiz in Training Featured Poster

Hey

Even if I did this..

for(int i=0; (i < N); i++) {
        
        for (int j=0; (j < M); j++)
        {
            matrix[i*N+j] = array[i+j];
           
            cout << matrix[i*N+j] << "\t";
    
        }
        cout << "\n";
    }

What should the numbers be print out for i+j? (Sorry to ask, just really confusing)

i+j in fixed:
0
1
2
3
4
1
2
3
4
5
2
3
4
5
6

phorce 131 Posting Whiz in Training Featured Poster

Hey thanks for the reply (You were right, they are doubles.. Not needed)

when I print out i+j it gives me:
0
1
2
1
2
3
2
3
4
3
4
5
4
5
6

Which looks weird.

phorce 131 Posting Whiz in Training Featured Poster

Here is some of the code I've been working on atm:

matrix = new double[N*M]; // dynamically assign the size of the matrix
    // This is the test matrix
    double array[15] = { 0, 1, 1, 
                          1, 0, 0,
                          1, 1, 1,
                          1, 1, 1, 
                          1, 1, 0
                        };
    
    for(int i=0; (i < M); i++) {
        
        for (int j=0; (j < N); j++)
        {
            matrix[i*N+j] = (double) array[i+j];
            cout << matrix[i*N+j] << "\t";
        
        }
        cout << "\n";
    }

When I try to output it though, it does this:

0 1 1
1 1 1
1 1 0
1 0 0
0 0 1

Which isn't right.. Any advice?

phorce 131 Posting Whiz in Training Featured Poster

Thanks for your reply.

I would like to know how to read a matrix from a text file into the 1d matrix..

If I had a file called: matrix1.txt, I could then read it into the matrix and output it in main..

If that makes sense =)

phorce 131 Posting Whiz in Training Featured Poster

Hello, I'm just wondering really..

I have a 2D matrix but I've defined it as a 1D matrix. So for example:

double *matrix = new double[N*M];

My question is, I'm going to be manipulating data very shortly and want to read this data in from a text file..

Say if the contents of the text file was:

0 1 0 1 0 0 0 0
0 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1

I know that it is possible, I'm just confused on how I would go about adding the rows and columns (into this style)

Any help would be great, thanks =)

phorce 131 Posting Whiz in Training Featured Poster

That's because you need to do something else (I'm pretty sure)..

For example, the code that you have WILL remove .php from the url's (www.abc.com/article.php -> /article) but, because you are requesting variables (using $_GET) you need to provide something else.

The .htaccess works by taking the url: /article/1/2 and just redirects it to: /article?id=1&page_no=2.. for example..

Let me give you an example: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Hope this helps you =)

phorce 131 Posting Whiz in Training Featured Poster

in the constructor:

public function __construct($theTitle, $theHeader, $theMast)
    	{
    		$this->title = $this->_title($theTitle);
    		$this->header_loc = $this->_header($theHeader);
    		$this->pageHeading = $theMast;
    	}

I've also tried to use a function to set it as well - doesn't work

phorce 131 Posting Whiz in Training Featured Poster

Fixed it :D This is really confusing me.. Any idea why $pageHeading is returning null?

<?php
    
    DEFINE ('HEADER_PATH', 'includes/headers/'); 
    class Page {
        
    	protected $title;
    	protected $header_loc;
    	protected $pageHeading;
    	
    	protected $header_file;
    	
    	public function __construct($theTitle, $theHeader, $theMast)
    	{
    		$this->title = $this->_title($theTitle);
    		$this->header_loc = $this->_header($theHeader);
    		$this->pageHeading = $theMast;
    	}
    	
    	public function setPageHeading($theMast)
    	{
    	   return $theMast;
    	}
    	public function _header($theHeader)
    	{	
                var_dump($this->pageHeading); // returns NUL
    		if(empty($theHeader))
    		{
    		   echo 'Error: You have not provided a header location';
    		}
    		
    		$this->header_file = (string) HEADER_PATH . $theHeader . ".php";
    		try {
    		  $page_title = $this->title;
    		  include ($this->header_file);
    		}catch(CusomException $e){
    		  if(!file_exists($file))
    		  {
    		     echo ('The specified file location does not exist!' + $e);
    		  }
    		}
    	}
    	
    	public function _title($theTitle)
    	{
    		if(empty($theTitle))
    		{
    			echo 'Error: You have not provided a page title';	
    		}
    		
    		if($theTitle == "[DYNAMICALLY ASSIGN]")
    		{
    		  $this->title = $this->_dynamically();
    		}else{
    		  $this->title = $theTitle;
    		}
    		
    		return $theTitle;
    	}
    	
    	public function _dynamically()
    	{
    		$title = 'Dynamically Assigned';
    		$this->title = $title;
    	}
    	
    	
    	public function _getTitle()
    	{
    		return $this->title;
    	}
    }
?>
phorce 131 Posting Whiz in Training Featured Poster

Thanks for your reply..

Could you give me a tiny example of what you mean? I'm sorry to ask, but, it's confusing me

phorce 131 Posting Whiz in Training Featured Poster

Hello hope that you can help.

Basically, I'm making a site that uses a header file and then for each page, include the header and set the title.. It's easy using procedural PHP but I want to do it using OO. Here is the problem:

Page.php (class)

<?php

    class Page {

        protected $title;
        protected $header_loc;
        protected $mast_desc;

        public function __construct($theTitle="", $theHeader="", $mast_head="")
        {


        }

        public function setTitle($theTitle)
        {
           $this->title = $theTitle;
        }

        public function _getTitle()
        {
            return $this->title;
        }
    }
?>

Header.php

<?php
  include ('Page.php');
  $header = new Page();

?>
<html class="no-js not-ie" lang="en"><!--<![endif]-->
<head>
<title><?php echo $header->_getTitle; ?></title>

Index.php

<?php
   include ('Page.php');
   $index = new Page('Welcome to the site', '/headers/header1.php', 'foo bar');
?>

Anyone see where I'm going wrong?

Thanks =)

phorce 131 Posting Whiz in Training Featured Poster

I already mentioned it =) Pass the variables through the function:

sum (a, b);

etc..

phorce 131 Posting Whiz in Training Featured Poster

It basically works on the variable types (int, string etc..) So when using === it basically checks to see if the types are the same.. Let me show you with an example:

<?php
	
	$number = (int) 1;
	$number2 = (string) "1";
	
	if($number == $number2)
	{
		echo '== Match';
	}else{
		echo '== Doesnt match';
	}
?>

This would return true, because the values are the same and we're only comparing the values.

<?php
	
	$number = (int) 1;
	$number2 = (string) "1";
	
	if($number === $number2)
	{
		echo 'They match';
	}else{
			echo '=== Doesnt match';
	}	
?>

This wouldn't match because number 1 is an int, and number 2 is a string.

Let me know if this helps you =)

karthik_ppts commented: Useful post +6
phorce 131 Posting Whiz in Training Featured Poster

hahaha =)!

I hope your problem is resolved now (Remember, if you create more functions - Pass the variables through!)

If you thread is solved, please make it solved by clicking the "Mark this thread as solved" and give rep points if you think someone helped!

Goodluck :)

phorce 131 Posting Whiz in Training Featured Poster

You're using visual studio 2010 professional :P

Have you tried/heard of Dev-Cpp? It's really good for starting out =)!

phorce 131 Posting Whiz in Training Featured Poster

Just take a screen show, and upload it via tinypic.com =)

phorce 131 Posting Whiz in Training Featured Poster

That is strange, what compiler are you using?

http://tinypic.com/r/avha1d/5

phorce 131 Posting Whiz in Training Featured Poster

Hey thanks for your reply..

EDIT: Just read your comment, thank you :) It works!

BUT the for loops are still confusing me

Thanks :)

phorce 131 Posting Whiz in Training Featured Poster

Why? Does it work, I don't understand?

phorce 131 Posting Whiz in Training Featured Poster

I don't get the memory location (I think that's what you mean)!

But:

#include <iostream>
using namespace std;

double array_of_5[5] = {1,2,3,4,5};
double sum(double a , double b );

double a= array_of_5[2];
double b= array_of_5[2];
int main ()
{
	
	cout<<"The sum of two members of the array is: " <<sum(a, b) << endl;

return 0;
}

double sum(double a, double b )
{
	return a + b;
}

You need to pass the values a and b for it to complete the calculation.

Hope this helps =)

phorce 131 Posting Whiz in Training Featured Poster

Hello could someone explain what I'm doing wrong to get the error: Segmentation fault: 11 when trying to fill in a 2D array from two for loops?

Here is the code:

Matrix.h

#ifndef _Matrix_h
#define _Matrix_h

using namespace std;
class Matrix
{
    public:
    
        Matrix(); // Constructor 
        Matrix(int M, int N);
    
        double getRead();

    protected:
    
    double **matrix;
    
    int rows;
    int columns;
};
#endif

Matrix.cpp

#include <iostream>
#include "Matrix.h"

using namespace std; 

Matrix::Matrix(){};

Matrix::Matrix(int M, int N)
{
    rows = M; 
    columns = N;
    
    double **matrix = new double*[rows*sizeof(double*)]; 
	for(int i = 0; i < rows; i++)
        matrix[i] = new double[columns]; // allocate columns for each row

    for(int i=0, j=0; (i < rows, j < columns); i++, j++)
    {
        matrix[i][0] = i;
        matrix[0][j] = j;
    }
}

double Matrix::getRead()
{
    
    for(int i=0, j=0; (i < rows, j < columns); i++, j++)
    {
        cout << matrix[i][j] << endl;
    }   
}

main.cpp

#include <iostream>
#include "Matrix.h"

using namespace std;

void readMatrix();

int main()
{
    Matrix m(5, 5);

    m.getRead();
    
    return 0;
}

Any help would be greatly appricated :)

phorce 131 Posting Whiz in Training Featured Poster

Think you'll find it uses both jQuery (Javascript) / PHP..

There isn't an actual way to do it in pure PHP.

phorce 131 Posting Whiz in Training Featured Poster

Hello I'm just wondering (In terms of computer science)

If someone gave you a task about matrix, 2D arrays to:

Assign to each of the aij, the value 1000 + i + j?

Would you assume that

the array would have:

rows = 500
columns = 500

That's the 1000 bit..

Then 2 for loops:

i = (i=0; (i < 500);
j = (j=0; (j < 500);

Would this be an adequate assumption?

Thanks :)

phorce 131 Posting Whiz in Training Featured Poster

I look so stupid now ;) sorry my brain has just been fried with all this!

Thank you so much for your help!

phorce 131 Posting Whiz in Training Featured Poster

Hey thank you so much for your reply.. BUT

#include <iostream>

using namespace std;

void changeArray(int rows, int columns)
{
	double **array = new double**[rows*sizeof(double*)]; 
	for(int i = 0; i < rows; i++)
	   array[i] = new double[columns]; // allocate columns for each row
	
}
int main(int argc, char *argv[]) {
	
	double rows = 10;
	double columns = 30;
	
	double *array; 
	
	changeArray(rows, columns);
}

Error:
Untitled.cpp: In function 'void changeArray(int, int)':
Untitled.cpp:7: error: cannot convert 'double***' to 'double**' in initialization

Am I completely missing the point? I hate 2D arrays :(!

phorce 131 Posting Whiz in Training Featured Poster

Hey, when I try your example.. It just throws up errors :(!

#include <iostream>

using namespace std;

void changeArray(int rows, int columns)
{
	double **array = new double[rows*sizeof(double*)];
	for(int i = 0; i < rows; i++)
	   array[i] = new double[columns]; // allocate columns for each row
	
}
int main(int argc, char *argv[]) {
	
	double rows = 10;
	double columns = 30;
	
	double *array;	
}

Untitled.cpp: In function 'void changeArray(int, int)':
Untitled.cpp:7: error: cannot convert 'double*' to 'double**' in initialization

Any ideas?

phorce 131 Posting Whiz in Training Featured Poster

Sorry, I'm confused..

matrix = new double[(unsigned int)rows*(unsigned int)columns];

Is 2D right?

e.g.

matrix = new double[10][20];

Also, would I set the values the same way (i.e. using the example loop you gave?)

Thanks :)

phorce 131 Posting Whiz in Training Featured Poster

Hello, I'm trying to create a matrix that dynamically defines the rows and columns.. Now, everything is fine, apart from when I include a function to output the 2D array..

The error:
Matrix.cpp:20: error: invalid types ‘double[int]’ for array subscript
...

The code is:

Matrix.cpp

#include <iostream>
#include "Matrix.h"

using namespace std; 

Matrix::Matrix(){};

Matrix::Matrix(double M, double N)
{
    rows = M;
    columns = N;
    matrix = new double[(unsigned int)rows*(unsigned int)columns];

}

double Matrix::getRead()
{
    for (int i=0, j=0; (i < rows, j < columns); i++, j++)
    {
        cout << matrix[i][j];   
    }    
}

Matrix.h

#ifndef _Matrix_h
#define _Matrix_h
#include <vector>

using namespace std;
class Matrix
{
    public:
    
        Matrix(); // Constructor 
        Matrix(const double M, const double N);
    
        double getRead();

    protected:
    
    double* matrix;
    
    int rows;
    int columns;
};


#endif

Any ideas? Thanks =)

phorce 131 Posting Whiz in Training Featured Poster

No problem :) If this thread is solved, please mark it and give reputation points to those who helped!

phorce 131 Posting Whiz in Training Featured Poster

Ahh just confused me even more :P!

There are currently no elements inside the vector (If that makes sense?)

Shall I implement two for loops or something? :S

Sorry

phorce 131 Posting Whiz in Training Featured Poster

Hello, I'm creating a Matrix, that the user can manually define the rows and columns.. (This will be defined in a class) But, I'm having trouble resizing the vector to what is set in main..

Here is the code:

Matrix.h

#ifndef _Matrix_h
#define _Matrix_h
#include <vector>

using namespace std;
class Matrix
{
    public:
    
        Matrix(); // Constructor 
        Matrix(double R, double C);
        double getSize();
        
        double readData(double row, double column);

    protected:
    vector<vector<double> > matrix;
    
    int rows;
    int columns;
};
#endif

Matrix.cpp

#include <iostream>
#include "Matrix.h"

using namespace std; 

Matrix::Matrix(){};

Matrix::Matrix(double M, double N)
{
    rows = M;
    columns = N;
    
    matrix.resize(rows);
    matrix.resize(columns);
    
    
}

double Matrix::getSize()
{
    return matrix.size();   
}

main.cpp:

#include <iostream>
#include "Matrix.h"

using namespace std;
int main()
{
    Matrix m(100, 300);
    
    cout << m.getSize();
    return 0;
}

Any ideas? Thank you =)

phorce 131 Posting Whiz in Training Featured Poster

Does the text completely decrypt now?

phorce 131 Posting Whiz in Training Featured Poster

I would personally go with the one you've gone for.. I.e:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c', 'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

Do the text analysis on the text file.

I think Thines01 can implement a way of switching the letters =)

phorce 131 Posting Whiz in Training Featured Poster

Do you mean reorganise the letter array into the most frequent? I've re done it to that now of:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c',
  'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

In essence, the way you've done it is "Hardcoded" and they should be sorted by means of like LINQ and not just a pre-defined array (This will make you pass your assignment, however, is not really a good way to program)

Your array should contain something like:

A = 8.2, B = 2.3 etc.. And then sorted that way.

Doing the method you've done, have you tried to match the the two values together? For example:

Alphabet: E T A I O N S R H U I C D F M Y G P W B V X Q K J Z
Ciphertext: C Y X S M P E T U V Q L I H K W J A D O R Z B G N

?

phorce 131 Posting Whiz in Training Featured Poster

Hey,

You have not included the frequency analysis itself - Just the alphabet array.

For example:

char[] aLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
                                 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

Needs to have the frequencies (1.4, 3.2, 2.2) etc and then ordered by the most frequent.

Hope this helps

phorce 131 Posting Whiz in Training Featured Poster

Hey thanks for the reply..

I have this now basically:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;
string CurrentPath;

string getCurrentPath();
 
int main(int argc, char** argv)
{
	cout << getCurrentPath();
	   return 0;
}
string getCurrentPath()
{
	size_t size;
		char *path=NULL;
	path=getcwd(path,size);
	CurrentPath=path;
	return path;
}

But when I run the program and output it, the output isn't displaying the file path to where the location is, just showing like:

/Users/[my_computer_name]

Any ideas?

phorce 131 Posting Whiz in Training Featured Poster

Hello, I'm working with files in a C++ project AND the text file that I need to open is in the same directory, however when just trying to open it like this:

string inputFile = "myText.txt";

It will not open, even though it's in the same directory, so then I have to put the file location (the directory path) which is fine BUT other people will be using this application and won't have access to my directory..

I want something that shows the current directory, like in C# there's a function for this.. I can't seem to find the same one in C++ so I've been working with this:

#include <stdio.h>
#include <dirent.h>
#include <iostream>
using namespace std;

int main (int c, char *v[]) {
 	
     DIR * mydir = opendir(".");

    cout << mydir;
    return 0;
}

But it just shows me the memory location, even if I have &mydir

Any suggestions? =)

phorce 131 Posting Whiz in Training Featured Poster

why is:

do($_POST['register']);

"do" usually associates with a do-while loop:

<?php

  do (something) 
  {

  }while($_POST['register'] = '');
?>

As an example!

phorce 131 Posting Whiz in Training Featured Poster

Technically a string is an array? Would this be ok:

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
	string std;
	
	cout << "Please enter your string: ";
	cin >> std;
	
	for(int i=0; (i < std.length()); i++)
	{
		cout << std[i] << endl;
		
	}
	return 0;	
}

- What do you plan to do with it?

=)

phorce 131 Posting Whiz in Training Featured Poster

Ahh thanks for the reply =)

I've tried to convert it to a string, still outputs the same.. It's really weird:

var theText = text.ToString();

etc..

phorce 131 Posting Whiz in Training Featured Poster

Hello I need some help, I'm trying to output something to a text file (.htm) which will then display the data that is being generated in the C# program..

Now the problem is, whenever I try to replace something, it doesn't work. Just replaces it with "System.Char[]"

Here is the code:

public static void WebPage(char[] text)
{
       var theText = text;
	
       string dir = Path.Combine(Directory.GetCurrentDirectory(), "index.htm");
	if(!File.Exists(dir))
	{
	     Console.WriteLine ("Cannot find the file");
	     return;
	}
			
	string webpage = getData (dir);
	FileStream fstream = new FileStream(dir, System.IO.FileMode.Create);
	StreamWriter sWriter = new StreamWriter(fstream);
			
	string variable = "var text="; // This is the variable in "index.htm"
	string replacement = variable + theText; // This is what I'm trying to replace it with 
	sWriter.Write(webpage.Replace(variable, replacement));
	sWriter.Flush();
}

So basically, let's say that the program generated the line "Hello world, this is C#", it would then search through the index.htm file and find "var text=" and then replace it with "var text=Hello world, this is C#"

Any ideas please? :)

phorce 131 Posting Whiz in Training Featured Poster

Have you tried:

$result = mysql_query("SELECT * FROM users WHERE uname = '$username' AND password = '$pass'") or die (mysql_error());