GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Sep-2004, 05:36
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
Exclamation

i can't find the wrong!!


hi everybody this code must create matrices that user inputs and add them if user wants but use only pointers no arrays are allowed!. . .
CPP / C++ / C Code:
#include <iostream.h>
#include<stdlib.h>

typedef int * matrix_member;

void ask_matrix_size(int *size);
matrix_member allocate_memory(int size);
matrix_member read_matrix(int size);
void print_matrix(matrix_member matrix,int size);
matrix_member create_matrix(int *size);
void add_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size);
matrix_member return_address(int i,int j,matrix_member m,int size);



int main () 
{

	int size=0;
	int choice=0;
	matrix_member matrix1;
	matrix_member matrix2 ;
	matrix_member result_matrix;

	matrix1=create_matrix( &size);
	matrix2=create_matrix (&size);

	print_matrix (matrix1,size);
	print_matrix (matrix2,size);

	while (choice!=3)
	{
		cout<<"==========MENU==========\n";
		cout<<"1)Add matrices\n";
		cout<<"2)Multiply matrices\n";
		cout<<"3)Exit\n";

		cout<<"Enter your choice:";
		cin>>choice;

		switch (choice) {

			case 1:
				add_matrix (matrix1,matrix2,result_matrix,size);
				break;
			case 2://multiplication will come here but first addition. . .
				
				break;
			case 3:
				cout<<"Thanks!... See you soon";
				cout<<"copyright by birben";
				return -1;
			default:
				cout<<"Choose the right number for your operation!";
		}
	}

	return 0;
}

void ask_matrix_size (int * size) //ask the user matrix size 
{
	cout<<"Enter the size of the matrix that you want to make operations";
	cin>> *size;
}

matrix_member allocate_memory (int size) //allocate memory for the array
{
	matrix_member start; //we define a integer pointer for the start of the array
	if(!(start=(matrix_member)calloc( (size*size) ,sizeof(matrix_member) ) )){
		cout<<"not enough memory";
		
	} 

	return start;
}

matrix_member read_matrix ( int size ) //this will take the matrix elements and place them inside it
{
	int i;
	matrix_member m;
	for (i=0; i<size*size ;i++) 
	{
		cout<<"Enter the number for the matrix then press enter";
		cin>>*(m+i);
		
	}
	
	return m;
} 

void print_matrix (matrix_member matrix, int size)
{
	int i;
	for (i=0 ;i<size ;i++)
	{
		cout<<*(matrix+i);
	}
}

matrix_member create_matrix (int * size)
{
	
	matrix_member matrix;
	ask_matrix_size (size);
	matrix=allocate_memory(*size);
	read_matrix(*size);

	return matrix;
}

matrix_member return_adress (int i,int j,matrix_member m, int size)//return the adress of the desired component of the matrix
{
	   return (m + ((i*size)+j));
}

void add_matrix(matrix_member m1,matrix_member m2,matrix_member result,int size)
{
	int i,j;
	for(i=0  ; i<size ;i++) {
		for( j=0 ;j<size ;j++) {
			
			*(return_adress(i,j,result,size) )= (* (return_adress(i,j,m1,size) ) ) + ( * (return_adress(i,j,m2,size) ) );
			
			cout<<*(return_adress(i,j,result,size) );

		}
	}
}
Last edited by dsmith : 20-Sep-2004 at 09:04. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 20-Sep-2004, 21:45
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
You need to give more information than "it doesn't work". What does it do wrong? Where is it going wrong? Add comments to pinpoint where you think the problem is.

And use code tags when you post your code. I'll bet moderators here get very tired of changing your posts to add code tags as Garth as done at least once and DSmith as done at least 4 times. Haven't you noticed your posts don't look the way you posted them and wondered why? Read the last line of each.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 21-Sep-2004, 00:05
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
Quote:
Originally Posted by WaltP
You need to give more information than "it doesn't work". What does it do wrong? Where is it going wrong? Add comments to pinpoint where you think the problem is.

And use code tags when you post your code. I'll bet moderators here get very tired of changing your posts to add code tags as Garth as done at least once and DSmith as done at least 4 times. Haven't you noticed your posts don't look the way you posted them and wondered why? Read the last line of each.
i am sorry about this i am very conufused nowadays so sorry again i don't forget the tags the next time. and i also solve my problems in the code.
SORRY AGAİN!...
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
something wrong with this code loon MySQL / PHP Forum 5 07-Jul-2003 06:55
HELP!: find window width? Center a <div>? Kingherc Web Design Forum 2 06-Jul-2003 06:25
How Do i get php to find out the file type of a file for me? viperman95833 MySQL / PHP Forum 2 08-Mar-2003 10:48
How do I find the Unix log file route? jrobbio Web Hosting Forum 4 04-Feb-2003 08:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.