Templates...???

B

Bore Biko

Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...

1)A program which creates a linked list of prim numbers..

/*--------------------------------*/
#include<stdio.h>
#include<malloc.h>
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k;
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(sizeof(struct lst));
prvi->next=(struct lst*)malloc(sizeof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prvi->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=prvi;
for(i=1;i<k;i++){
exit:
tekuci_drugi=prvi;
while(tekuci_drugi->next){
if((i%(tekuci_drugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=tekuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_prvi=tekuci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n",i);*/
}
tekuci_prvi=prvi;
while(tekuci_prvi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tekuci_prvi->next;

}
exit(0);
}



2)A program which creates a file(if you redirect output with ">")
of random numbers use:"rand > file.txt", and "file.txt" is input for third
program...

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

int main(int argc, char *argv[])
{ int i, j, k;
k=atoi(argv[1]);
srand(k);
for(i=0;i<k;i++){
printf("%d\n",rand());
}
exit(0);
}

3)A program which reads a list of random numbers generated with second
program a "file.txt" and make a sorted list of them...

#include<stdio.h>
#define BUFFER 128

struct lst{
int value;
struct lst *next;
};

int main(int argc , char *argv[]){
int i, j ,k;
FILE *fp;
char buffer[BUFFER];
struct lst *prvi, *tekuci_jedan, *tekuci_dva, *zadnji;
fp=(fopen(argv[1],"r"));
if(fp==(FILE*)NULL){
printf("\nError can't open:%s",argv[1]);
}
prvi=(struct lst*)malloc(sizeof(struct lst));
tekuci_jedan=prvi;
while(fgets(buffer, BUFFER+1, fp)){
tekuci_jedan->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_jedan->value=atoi(buffer);
tekuci_jedan=tekuci_jedan->next;
}
tekuci_jedan->next=(struct lst*)NULL;
tekuci_jedan=prvi;
while(tekuci_jedan->next){
tekuci_dva=prvi;
while(tekuci_dva->next){
if(tekuci_jedan->value < tekuci_dva->value){
/* i=tekuci_jedan->value; */
tekuci_jedan->value=tekuci_jedan->value+tekuci_dva->value;
tekuci_dva->value=tekuci_jedan->value-tekuci_dva->value;
tekuci_jedan->value=tekuci_jedan->value-tekuci_dva->value;
}
tekuci_dva=tekuci_dva->next;
}
tekuci_jedan=tekuci_jedan->next;
}
tekuci_jedan=prvi;
while(tekuci_jedan->next){
printf("\n%d",tekuci_jedan->value);
tekuci_jedan=tekuci_jedan->next;
}
exit(0);
}


All programs are commpilled with gcc without any waring...

Thank in advance, Robert...!!!

P.S. I don't know does a gcc support templates if not you can
send mee a version for VC++ 6.0
 
D

Dave Rahardja

Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...

Templates in C++ is just a tool that you can use to solve certain kinds of
problems. The programs that you have included do not belong to that class of
problems and are therefore not likely to benefit from templates.

Templates allow you to program in a style called "generic programming", a
style that lets you use types as a compile-time parameter in your program.

For example, here's a function that adds two numbers:

template <class T>
T add(T a, T b)
{
return a + b;
}

And here's a program that calls it:

int main()
{
double a = add(0.0, 1.0);
int b = add(1, 2);
return 0;
}

The compiler will automatically generate two instances of add(), one that
deals with doubles, and another that deals with ints. In fact, you can pass as
parameters any class that has the operator + defined (and allow access to the
appropriate assignment and copy constructors).

-dr
 
K

Kai-Uwe Bux

Bore said:
Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...

I tried to keep the spirit of the algorithm and a little bit of the style.
1)A program which creates a linked list of prim numbers..

/*--------------------------------*/
#include<stdio.h>
#include<malloc.h>
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k;
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(sizeof(struct lst));
prvi->next=(struct lst*)malloc(sizeof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prvi->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=prvi;
for(i=1;i<k;i++){
exit:
tekuci_drugi=prvi;
while(tekuci_drugi->next){
if((i%(tekuci_drugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=tekuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_prvi=tekuci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n",i);*/
}
tekuci_prvi=prvi;
while(tekuci_prvi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tekuci_prvi->next;

}
exit(0);
}

#include <iostream>
#include <list>
#include <cstdlib>

typedef std::list< unsigned long > UlongList;

int main ( unsigned int argn, char* args[] ) {
unsigned long upper_bound = std::atoi( args[1] );
UlongList primes;
for ( unsigned long candidate = 2; candidate < upper_bound; ++candidate )
{
for ( UlongList::const_iterator iter = primes.begin();
iter != primes.end(); ++ iter ) {
if ( 0 == candidate % *iter ) {
goto failed;
}
}
std::cout << candidate <<'\n';
primes.push_back( candidate );
failed : ;
}
}


This produces slightly different output: just the prime numbers strictly
less than the command line argument are printed in ascending order.

Note: 1 is not a prime number.
2)A program which creates a file(if you redirect output with ">")
of random numbers use:"rand > file.txt", and "file.txt" is input for third
program...

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

int main(int argc, char *argv[])
{ int i, j, k;
k=atoi(argv[1]);
srand(k);
for(i=0;i<k;i++){
printf("%d\n",rand());
}
exit(0);
}

#include <iostream>
#include <cstdlib>

int main ( unsigned int argn, char* args[] ) {
unsigned long count = std::atoi( args[1] );
std::srand( count );
for ( unsigned long i = 0; i < count; ++i ) {
std::cout << std::rand() << '\n';
}
}


This version does not look that much different at all.

3)A program which reads a list of random numbers generated with second
program a "file.txt" and make a sorted list of them...

#include<stdio.h>
#define BUFFER 128

struct lst{
int value;
struct lst *next;
};

int main(int argc , char *argv[]){
int i, j ,k;
FILE *fp;
char buffer[BUFFER];
struct lst *prvi, *tekuci_jedan, *tekuci_dva, *zadnji;
fp=(fopen(argv[1],"r"));
if(fp==(FILE*)NULL){
printf("\nError can't open:%s",argv[1]);
}
prvi=(struct lst*)malloc(sizeof(struct lst));
tekuci_jedan=prvi;
while(fgets(buffer, BUFFER+1, fp)){
tekuci_jedan->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_jedan->value=atoi(buffer);
tekuci_jedan=tekuci_jedan->next;
}
tekuci_jedan->next=(struct lst*)NULL;
tekuci_jedan=prvi;
while(tekuci_jedan->next){
tekuci_dva=prvi;
while(tekuci_dva->next){
if(tekuci_jedan->value < tekuci_dva->value){
/* i=tekuci_jedan->value; */
tekuci_jedan->value=tekuci_jedan->value+tekuci_dva->value;
tekuci_dva->value=tekuci_jedan->value-tekuci_dva->value;
tekuci_jedan->value=tekuci_jedan->value-tekuci_dva->value;
}
tekuci_dva=tekuci_dva->next;
}
tekuci_jedan=tekuci_jedan->next;
}
tekuci_jedan=prvi;
while(tekuci_jedan->next){
printf("\n%d",tekuci_jedan->value);
tekuci_jedan=tekuci_jedan->next;
}
exit(0);
}

#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

typedef std::vector< int > IntVector;

int main ( unsigned int argn, char* args[] ) {
std::fstream in_file ( args[1] );
if ( ! in_file ) {
std::cerr << "File " << args[1] << " could not be opened.\n";
} else {
IntVector i_vect;
int number;
while ( in_file >> number ) {
i_vect.push_back( number );
}
std::sort( i_vect.begin(), i_vect.end() );
std::copy( i_vect.begin(), i_vect.end(),
std::eek:stream_iterator< int >( std::cout, "\n" ) );
}
}


Here, I changed the data structure to be a vector instead of a list. Using a
list the program would read:

#include <fstream>
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

typedef std::list< int > IntList;

int main ( unsigned int argn, char* args[] ) {
std::fstream in_file ( args[1] );
if ( ! in_file ) {
std::cerr << "File " << args[1] << " could not be opened.\n";
} else {
IntList i_list;
int number;
while ( in_file >> number ) {
i_list.push_back( number );
}
i_list.sort();
std::copy( i_list.begin(), i_list.end(),
std::eek:stream_iterator< int >( std::cout, "\n" ) );
}
}


Notice the subtle change in calling the sort algorithm.

P.S. I don't know does a gcc support templates if not you can
send mee a version for VC++ 6.0

g++ supports templates in all recent versions.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top