String Array in C?

trying to become a little advanced

Page 1 of 1

9 Replies - 1354 Views - Last Post: 20 February 2009 - 09:22 AM Rate Topic: -----

#1 ancona0721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-February 09

String Array in C?

Posted 19 February 2009 - 01:51 PM

Just so everyone is clear, I am a University of Phoenix student, however, I do not need help with my homework. My code already does what the assignment states. I just want to see if I can take it a little bit further. I am using the Miracle C compiler that the school gives out for free.

Here is my code:



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

//Constant Variables for Tax
#define DELMAR_Tax 0.0725
#define ENCINITAS_Tax 0.075
#define LAJOLLA_Tax 0.0775

//function prototypes

float SalesTaxCalc(float, float);
float TotalCalc(float, float);
void clearScreen();
void pause(int);
//I am actually working on this part, and it is the part that it the primary focus of my question.
//char caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};


/* I would like to create a one dimensional array which would have each location in each element of the array.

When it got down to actually showing the output, I would rather it roll through the array instead of printing each individual line separately.

Also, can you use an array to display the variables? For example, could I create an array to roll through my constant variables for display later? If this isn't making sense, please let me know so I may be able to explain a little better.
*/

//main function
main ()
{
//variable declaration and initialization
float fSubTotal = 0.0;
int iInput;

//Output information
printf("\n\t\t\tKudler Fine Foods\n");

do
{
printf("\nHow much did your purchase come to: ?", );
iInput = scanf("%f", &fSubTotal); //scanf() returns 0 on error and 1 on success
fflush(stdin); //clear out the input buffer in case the user entered data characters after the float value.
if (iInput == 0 )
{
//Invalid input
printf("\nThe amount entered is not a valid input.");
pause(2);
clearScreen();
}
else
{
//Valid input
printf("\nThe purchase amount came to $%.2f.\n", fSubTotal);
pause(2);
clearScreen();
} // end if... statement
} while ( iInput == 0); //end do... while loop



printf("\nDel Mar: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, DELMAR_Tax)), SalesTaxCalc(fSubTotal, DELMAR_Tax));

pause(2);
clearScreen();
printf("\nEncinitas: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, ENCINITAS_Tax)), SalesTaxCalc(fSubTotal, ENCINITAS_Tax));
printf("\nLa Jolla: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, LAJOLLA_Tax)), SalesTaxCalc(fSubTotal, LAJOLLA_Tax));
getch();
}

//define functions

//calculates sales tax by multiplying the subtotal by the tax percentage
float SalesTaxCalc (float fSubTotal, float fTaxRate)
{
return fSubTotal * fTaxRate;
}

//calculates the total by adding the tax to the subtotal
float TotalCalc (float fSubTotal, float fTax)
{
return fSubTotal + fTax;
}

//clear screen
void clearScreen()
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}


Is This A Good Question/Topic? 0
  • +

Replies To: String Array in C?

#2 ancona0721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-February 09

Re: String Array in C?

Posted 19 February 2009 - 02:05 PM

This is the most up to date version. Completely running.

This is what I want:

printf("\narray locations[x]: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, array for varbiable[x])), SalesTaxCalc(array for varbiable[x]));

Is this possible?


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

//Constant Variables for Tax
#define DELMAR_Tax 0.0725
#define ENCINITAS_Tax 0.075
#define LAJOLLA_Tax 0.0775

//function prototypes

float SalesTaxCalc(float, float);
float TotalCalc(float, float);
void clearScreen();
void pause(int);
//char caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};


//main function
main ()
{
//variable declaration and initialization
float fSubTotal = 0.0;
int iInput;

//Output information
clearScreen();
printf("\n\t\t\tKudler Fine Foods\n");

do
{
printf("\nHow much did your purchase come to: ?", );
iInput = scanf("%f", &fSubTotal); //scanf() returns 0 on error and 1 on success
fflush(stdin); //clear out the input buffer in case the user entered data characters after the float value.
if (iInput == 0 )
{
//Invalid input
printf("\nThe amount entered is not a valid input.");
pause(2);
}
else
{
//Valid input
printf("\nThe purchase amount came to $%.2f.\n", fSubTotal);
pause(2);
} // end if... statement
} while ( iInput == 0); //end do... while loop

printf("\nDel Mar: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, DELMAR_Tax)), SalesTaxCalc(fSubTotal, DELMAR_Tax));
pause(3);
printf("\nEncinitas: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, ENCINITAS_Tax)), SalesTaxCalc(fSubTotal, ENCINITAS_Tax));
pause(3);
printf("\nLa Jolla: The total for a $%.2f purchase is $%.2f after $%.2f in taxes.\n", fSubTotal, TotalCalc(fSubTotal, SalesTaxCalc(fSubTotal, LAJOLLA_Tax)), SalesTaxCalc(fSubTotal, LAJOLLA_Tax));
pause(3);

printf("\nPress <ENTER> to exit");
getch();
}

//define functions

//calculates sales tax by multiplying the subtotal by the tax percentage
float SalesTaxCalc (float fSubTotal, float fTaxRate)
{
return fSubTotal * fTaxRate;
}

//calculates the total by adding the tax to the subtotal
float TotalCalc (float fSubTotal, float fTax)
{
return fSubTotal + fTax;
}

//clear screen
void clearScreen()
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}

//pause function
void pause(int inNum)
{

int iCurrentTime = 0;
int iElapsedTime = 0;

iCurrentTime = time(NULL);

do {

iElapsedTime = time(NULL);

} while ( (iElapsedTime-iCurrentTime) < inNum );
clearScreen();
} //end pause function

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: String Array in C?

Posted 19 February 2009 - 02:05 PM

There's no way to have a string array in C as a one dimensional array, aside from one long array that has a space between each "element". Wouldn't be worth it. The next best option is a 2D array like so:

char strings[row][column];
//where row is simply 0-size and column is the # of chars in each "string"


Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: String Array in C?

Posted 19 February 2009 - 02:10 PM

not sure what you are trying to do, is it something like
char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};

int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
}


where caLocation[] is an array of pointers to char - in this case pointing to C strings

This post has been edited by horace: 19 February 2009 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: String Array in C?

Posted 19 February 2009 - 02:12 PM

View Posthorace, on 19 Feb, 2009 - 02:10 PM, said:

not sure what you are trying to do, is it something like
char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};

int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
}


where caLocation[] is an array of pointers to char - in this case pointing to C strings



That's faking one dimension though. A char pointer can hold any number of chars so that is effectively 2D. Just saying ;)
Was This Post Helpful? 0
  • +
  • -

#6 ancona0721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-February 09

Re: String Array in C?

Posted 19 February 2009 - 02:33 PM

View Posthorace, on 19 Feb, 2009 - 01:10 PM, said:

not sure what you are trying to do, is it something like
char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};

int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
}


where caLocation[] is an array of pointers to char - in this case pointing to C strings



Something like this

Can I have an array with my variables in them that would roll through and call each variable as i went through the array?

i will try to put some type of example. may not work, but it's just a thought process:

//populations
#define CINCINNATI_PEOPLE 300451
#define COLUMBUS_PEOPLE 612421
#define CLEVELAND_PEOPLE 425613

//function prototypes
char Statement(int);

char caCity[3] = {"Cincinnati", "Columbus", "Cleveland"};
char caPopulation[3] = {"CINCINNATI_PEOPLE", "COLUMBUS_PEOPLE", "CLEVELAND_PEOPLE"};

main()
{
int x;

for ( x = 0; x < 3; x++ );
Statement(x);
} //end main function

//define Statement function
char Statement(int x)
{
printf("\nThere are %d people in %c!", caPopulation[x], caCity[x]);
//Hopefully the output would be "There are 300451 people in Cincinnati!" the first time through.
//The Second time through would say "There are 612421 people in Columbus!"
//The Third time through would say "There are 425613 people in Cleveland!"
}

Again, not sure if at all possible, but just an idea.

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: String Array in C?

Posted 19 February 2009 - 02:57 PM

It's possible:
#include <stdio.h>

#define CINCINNATI_PEOPLE 300451
#define COLUMBUS_PEOPLE 612421
#define CLEVELAND_PEOPLE 425613

const char *caCity[3] = {"Cincinnati", "Columbus", "Cleveland"};
const int caPopulation[3] = {CINCINNATI_PEOPLE, COLUMBUS_PEOPLE, CLEVELAND_PEOP\
LE};


int main()
{
   size_t num_cities = sizeof(caCity)/sizeof(*caCity);
   size_t num_pops = sizeof(caPopulation)/sizeof(*caPopulation);

   size_t i = 0;
   for (; i < num_cities && i < num_pops; ++i)
   {
      printf("%s has %d citizens\n", caCity[i], caPopulation[i]);
   }
   return 0;
}

Was This Post Helpful? 1
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: String Array in C?

Posted 20 February 2009 - 08:46 AM

why not use a structure, e.g.
#include <stdio.h>

typedef struct
{
	char *name;
	int  population;
} City;

City london={"London", 100000};

City cities[]={{"London", 100000},{"Paris", 200000},{"Rome", 300000}};
int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("city %s population %d\n", cities[i].name, cities[i].population);

}


Was This Post Helpful? 0
  • +
  • -

#9 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: String Array in C?

Posted 20 February 2009 - 09:20 AM

View PostKYA, on 19 Feb, 2009 - 08:12 PM, said:

View Posthorace, on 19 Feb, 2009 - 02:10 PM, said:

not sure what you are trying to do, is it something like
char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};

int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
}


where caLocation[] is an array of pointers to char - in this case pointing to C strings



That's faking one dimension though. A char pointer can hold any number of chars so that is effectively 2D. Just saying ;)

but they have different effects, e.g. in
char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};


"Del Mar", "Encinitas" and "La Jolla" are constants - if you try to assign a new value to an element, e.g.
	caLocation[1][0]='x';


you will get a segmentation error and the program will halt
in fact one should declare the array so
const char *caLocation[3] = {"Del Mar", "Encinitas", "La Jolla"};


so the compiler would regect any assignment with an error message such as "assignment to a read-only location"

if we define a 2D array
#include <stdio.h>

char caLocation[3][20] = {"Del Mar", "Encinitas", "La Jolla"};

int main()
{
	int i;
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
	caLocation[1][0]='x';			   // attempt to change a character
	for (i=0;i<3;i++)
	  printf("%s\n", caLocation[i]);
}


the assigment works OK as "Del Mar", "Encinitas", and "La Jolla" are only initial values and may be overwritten.
It all depends what you are trying to do. If your data is constant use the former method if it may change use the latter. Particularly important in embedded systems where RAM is at a premium.

This post has been edited by horace: 20 February 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: String Array in C?

Posted 20 February 2009 - 09:22 AM

I'm not disagreeing on any particular point, but you didn't make the char* array const in your original post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1