Newbie:Considering using Python. Arrays?, Pointers?

Tom tom22 at verio.com
Fri May 21 15:48:43 EDT 1999



Randall Hopper wrote:

> Tom:
>  |I am considering using Python for some programming tasks as opposed to
>  |C, C++ etc, which I have used in the past. I bought the book,
>  |"Programming Python" and I was attempting to find out what kind of
>  |"structure/function" Python uses in terms of indexing data arrays which
>  |is equivalent to "C" pointers. I did not read the book thoroughly but I
>  |did not come up with anything. So, what does Python use for indexing
>  |arrays?
>
> I hear what you're saying, but I'm not to sure what the real underlying
> question is.  In C, you can use pointers and integers to access arrays
> (a[3], *(a+3), a[2][5], etc.).  In Python, you'd write this similarly:
> a[3], a[2][5].  Pointers aren't exposed as a programmer mungable (mutable)
> data type.
>
> Possibly you could rephrase with some short example code and we could draw
> some parallels.
>

Attached is a small "C" program that I wrote years ago that used pointers to
"linked lists" of "structures" (it finds the best match for the parallel
combination of two resistors...that should give away my real profession...E.E).
Admittedly, it is not very readable and would probably be less readable in
PERL. Also, the output data formatting is dreadfull and at one point I think I
just gave up making it "prettier".


>
>  |Also, Since Python is an interperted language, as is , say, Java, then
>  |what would be the advantage of using Python instead of Java?
>
> I'm assuming this isn't flame bait :-)

Heavens no. Just an honest question from a non-professional programmer.

Thank you for the reply. Actually, as opposed to file manipulation and "shell"
type scripting I do a lot of manipulation of mathematical functions and
vectors/arrays. The truth is, while I have programmed extensively in C and some
in PERL  I have not programmed at all in Java or Python.

It's not really fruitful to try and

> give a meaningful answer to this question without more details.  What class
> of applications are we talking about?  What is the required set of
> architectures you need to support?  How much time is available for the
> project, or is this a personal learning project?  What is the expected
> lifetime of the product?  Do we need to be able to interface with legacy C
> or C++ code libraries?  Does it need to run in web browser?  Does it have a
> GUI?  etcetc.  More details would allow folks to give some specific advice
> and offer comparisons.
>
> Randall
-------------- next part --------------
/* This program finds the parallel combination of two resistors. */
/* that most closely matches a resistor value which is input .*/
/* The resultant resistor values are from the set which define */
/* the resistor inventory. */
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define TRUE 1

void del(void);
void readlist(void);
void newdata(void);
void storlist(void);
void display(void);
void pfind(void);
void print(void);

struct data
{ float rval;
  char inventory;
  struct data *ptrnext;
};

struct data *ptrfirst, *ptrthis, *ptrnew, *ptrlast;
struct data *ptrsort[500];
struct data *temp;

char  dummy;
float desired,in,missing,multiplier,virtualr;
float result,error,nearest,resultl,resulth,lowval;
main()

{
int ch;
ptrfirst = (struct data *)NULL;
/*printf("\f");*/

puts("\t\tWELCOME TO THE PARALLEL RESISTOR FINDER PROGRAM\n");
puts("Just follow the prompts for directions concerning");
puts("entering, saving, displaying, and deleting items");
puts("from the database.\n");

	while (TRUE)
	{
	puts("\n\t\t\tYour Options Are:\n");
	puts("Type 'e' to enter new resistor values to the data base:");
	puts("Type 'w' to write the updated data base to a file called RBASE :");
	puts("Type 'r' to read the data base into memory:");
	puts("Type 'd' to delete a value from the database:");
	puts("Type 'l' to display the database to the console:");
	puts("Type 'f' to find a parallel resistor pair:");
	puts("Type 'p' to get a hardcopy of the database:");
	puts("Type 'q' to exit the program:");
	printf("\nENTER SELECTION:");
	ch = getche();
	switch (ch)
	{
		case 'p':
		print();  break;
		case 'q':
		exit ();
		case 'f':
		pfind();  break;
		case 'l':
		display(); break;
		case 'e':
		newdata(); break;
		case 'w':
		storlist();break;
		case 'r':
		readlist();break;
		case 'd':
		del();	   break;		
		default:
		puts("\nEnter only the selections listed");
		}	/* end switch */
	}	/* end while */
}	/* end main */


void newdata(void)

{

/* put the data into the structure */

ptrnew = ( struct data *) calloc(1,sizeof(struct data)) ;
if (ptrfirst == (struct data *)NULL)
	ptrfirst = ptrthis = ptrnew;
else
	{ptrthis = ptrfirst;
	while(ptrthis->ptrnext != (struct data *)NULL)
		ptrthis = ptrthis->ptrnext;
	ptrthis->ptrnext = ptrnew;
	ptrthis = ptrnew;
	}
printf("\nEnter new resistor value:");
scanf("%f",&ptrthis->rval);
fflush(stdin);
printf("\nEnter 'i' for inventory or 'n' for not inventory:");
ptrthis->inventory = getchar();
fflush(stdin);
ptrthis->ptrnext = (struct data *)NULL;

}



void storlist(void)
{
FILE *fptr;							

/* initialize an array of pointers to structures of the type data */

int count = 0;
int in,out,lenlst;


/* Set the pointers that are inside the structures into the array */

	ptrthis = ptrfirst;
	ptrsort[count++] = ptrfirst;
	while (ptrthis->ptrnext != (struct data *)NULL)
		{ptrsort[count++] = ptrthis->ptrnext;
		ptrthis = ptrthis->ptrnext;}
lenlst = (count -1);


/* Sort the array of pointers */

for (out = 0; out < lenlst ;out++)				
	for (in = out + 1;in < lenlst + 1; in++)			
	if (ptrsort[out]->rval > ptrsort[in]->rval)	
			{temp = ptrsort[in];			
			ptrsort[in] = ptrsort[out];		
			ptrsort[out] = temp; }			



/* Put the sorted pointers back into the structure */

ptrfirst = ptrsort[0];					
ptrthis = ptrfirst;						
for (out = 1; out < (lenlst + 1); out++)				
	{ptrthis->ptrnext = ptrsort[out];			
	ptrthis = ptrthis->ptrnext; }				
ptrsort[lenlst]->ptrnext = (struct data *)NULL;		

	
/* put the data into a file  */					

fptr = fopen("rbase.dat","wb");				
if (ptrfirst == (struct data *)NULL)				
	{printf("\nEmpty list"); return;}			
ptrthis = ptrfirst;						
do								
	{							
	fwrite(ptrthis,sizeof(struct data),1,fptr);
	ptrthis = ptrthis->ptrnext;				
	}							
while (ptrthis != (struct data *)NULL);			
	fclose(fptr);						

						
}

void readlist(void)
{
FILE *fptr;
ptrnew = (struct data *)calloc(1,sizeof(struct data));
ptrfirst =ptrthis =ptrnew;
fptr = fopen("rbase.dat","rb");

while(fread(ptrthis,sizeof(struct data),1,fptr) == 1)
	{ptrthis->ptrnext = (struct data *)calloc(1,sizeof(struct data));
	ptrlast = ptrthis;
	ptrthis = ptrthis->ptrnext;}					
ptrlast->ptrnext = (struct data *)NULL;
fclose(fptr);


}


void del(void)
{
float rdel;

readlist();

puts("\nEnter the value of the resistor to be deleted from the database:");
scanf("%f",&rdel);
ptrthis = ptrfirst;
ptrlast = ptrthis;
while(ptrthis != (struct data *)NULL)
	{if(ptrthis->rval == rdel)
		{if(ptrthis == ptrfirst)
			{ptrfirst = ptrfirst->ptrnext;
			ptrthis = ptrfirst;
			puts("\nString Deleted Successfully");
			}
		else	{ptrlast->ptrnext = ptrthis->ptrnext;
			ptrlast = ptrthis;
			ptrthis = ptrthis->ptrnext;
			puts("\nString Deleted Successfully");
			}
		}
	else	{ptrlast = ptrthis;
		ptrthis = ptrthis->ptrnext;
		}
	}


storlist();	/* writes the file to disk */
}




void display(void)
{
ptrthis = ptrfirst;
do
{
printf("\n%.2f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
printf("%.2f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
printf("%.2f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
printf("%.2f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
printf("%.2f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
}
while(ptrthis != (struct data *)NULL);
printf("\nHit return to continue");
fflush(stdin);
dummy = getchar();

}




void print(void)
{
ptrthis = ptrfirst;
do
{
fprintf(stdprn,"\n%f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
fprintf(stdprn,"%f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
fprintf(stdprn,"%f %c     ",ptrthis->rval,ptrthis->inventory);
ptrthis = ptrthis->ptrnext;
}
while(ptrthis != (struct data *)NULL);
}


void pfind(void)
{

ptrthis = ptrfirst;
printf("\nEnter R_desired:");
scanf("%f",&desired);

lowval = ptrfirst->rval;
multiplier = 0.0;

virtualr = (ptrthis->rval)*pow(10.0,multiplier);



while(virtualr <= desired) /* find initial rin */

{
	while((virtualr <= desired) && (ptrthis != (struct data *)NULL))
	{
	do
	{
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
	while((ptrthis->inventory != 'i') && (ptrthis != (struct data *)NULL));
	
	}
	if(virtualr <= desired)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
}
	in = virtualr;		   
	missing = (desired*in)/(in-desired);/* find missing based on  rin */

while(missing > 1000000.0) /* if missing > 1e6 then raise rin to lower missing */
{
	while((missing > 1000000.0) && (ptrthis != (struct data *)NULL))
	{
	do
	{
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	in = virtualr;
	missing = (desired*in)/(in-desired);
	}
	while((ptrthis->inventory != 'i') && (ptrthis != (struct data *)NULL));
	}
	if(missing > 1000000.0)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	in = virtualr;
	missing = (desired*in)/(in-desired);
	}

}

if(in > 1000000.0)
	{puts("Resistor values are outside the normal range!");return;}

while(virtualr < missing ) 
{
	while((virtualr < missing) && (ptrthis != (struct data *)NULL))
	{
	lowval = virtualr;
	do
	{
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
	while((ptrthis->inventory != 'i') && (ptrthis != (struct data *)NULL));
	}
	if(virtualr < missing)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}


}

resultl = ((in*lowval)/(in+lowval));
resulth = ((in)*(virtualr)/(in+(virtualr)));

if(fabs(((desired/resultl)*100.0)-100.0) < (fabs(((desired/resulth)*100.0)-100.0)))
	{
	nearest = lowval;
	error = (fabs(((desired/resultl)*100.0)-100.0));
	result = resultl;
	}

else
	{
	nearest = (virtualr);
	error = (fabs(((desired/resulth)*100.0)-100.0));
	result = resulth;
	}


printf("\nThe following are resistors in NVISION inventory.");
printf("\nThe desired value = %.2f ohms",desired);
printf("\nR_1 = %.2f ohms",in);
printf("\nR_2 = %.2f ohms",nearest);
printf("\nThe result = %.2f ohms",result);
printf("\nThe error = %f percent",error);
printf("\n");

ptrthis = ptrfirst;
lowval = ptrfirst->rval;
multiplier = 0.0;
virtualr = (ptrthis->rval)*pow(10.0,multiplier);

while(virtualr <= desired) /* find initial rin */

{
	while((virtualr <= desired) && (ptrthis != (struct data *)NULL))
	{
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
	if(virtualr <= desired)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
}
	in = virtualr;		   
	missing = (desired*in)/(in-desired);/* find missing based on  rin */

while(missing > 1000000.0) /* if missing > 1e6 then raise rin to lower missing */
{
	while((missing > 1000000.0) && (ptrthis != (struct data *)NULL))
	{
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	in = virtualr;
	missing = (desired*in)/(in-desired);
	}
	if(missing > 1000000.0)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	in = virtualr;
	missing = (desired*in)/(in-desired);
	}

}

if(in > 1000000.0)
	{puts("Resistor values are outside the normal range!");return;}

while(virtualr < missing ) 
{
	while((virtualr < missing) && (ptrthis != (struct data *)NULL))
	{
	lowval = virtualr;
	ptrthis = ptrthis->ptrnext;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}
	if(virtualr < missing)
	{
	multiplier = multiplier + 1.0;
	ptrthis = ptrfirst;
	virtualr = (ptrthis->rval)*pow(10.0,multiplier);
	}


}

resultl = ((in*lowval)/(in+lowval));
resulth = ((in)*(virtualr)/(in+(virtualr)));

if(fabs(((desired/resultl)*100.0)-100.0) < (fabs(((desired/resulth)*100.0)-100.0)))
	{
	nearest = lowval;
	error = (fabs(((desired/resultl)*100.0)-100.0));
	result = resultl;
	}

else
	{
	nearest = (virtualr);
	error = (fabs(((desired/resulth)*100.0)-100.0));
	result = resulth;
	}


printf("\nThe following are resistors available on order.");
printf("\nThe desired value = %.2f ohms",desired);
printf("\nR_1 = %.2f ohms",in);
printf("\nR_2 = %.2f ohms",nearest);
printf("\nThe result = %.2f ohms",result);
printf("\nThe error = %f percent",error);
}




More information about the Python-list mailing list