473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Templates...???

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




Sep 10 '05 #1
2 1575
On Sat, 10 Sep 2005 01:47:26 +0200, "Bore Biko" <bo*******@yahoo.co.uk> wrote:
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
Sep 10 '05 #2
Bore Biko wrote:
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::ostream_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::ostream_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
Sep 10 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.