Thread: Please I need Help with arrays

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Help with arrays

    I am having problems with arrays. I entered it into the program but, having problewms getting it to compile. I don't know if it's inthe wrong place or if anything is missing. I appreciate any and all help. Thank you.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Go through the source and lay it out proplerly, then you'll see some obvious errors, like what's this doing:
    >>double fnCurrencyConversion(...
    It appears to be in the middle of the main function?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Please I need Help with arrays

    I am beginner at this. I having problems with arrays in the program below. I don't know if I am applying the functions correctly or putting things in the right place. I appreciate any and all help. Thank you.

    Code:
    #include <stdio.h>
    void ArrayElement(int[]);  /* passing array to function by reference */
    void menu(int*C);
    
    double fnCurrencyConversion(double dollars,double conversion_rate)
    {
    	return dollars * conversion_rate;
    
    }
    /* main program */
    int main (void)
    {
    
    	/* local variables for main() */
    
    	double conversion_rate;
    	double dollars;
    	double result;
    	int choice;
    	int j;
    	
    	int array[5]={0,1,2,3,4}; /* this array contains five elements */
    	/* array [0]=1.48655, array[1]=1.166, array[2]=1.3469, array[3]=1.371, array [4]=6.5485
    		show original array elements */
    	printf("Original array elements:\n");
    	for (j=0; j<5; j++) /*For Loop to manilpualate array elements */
    	{
    		printf("Array element #%d has a value of %d\n",j, array[j]);
    {	
    	double conversion_rate[5]={1.48655,1.166,1.3469,1.371,6.5485};
    
    
    	/* display menu */
    	/* call function passing by reference */
    
    	menu(&choice);
    	/* display user selection */
    	printf("Please enter dollar amount to convert:\n");
    	scanf("%1f",&dollars);
    	
    	result=fnCurrencyConversion(dollars,conversion_rate);
    	printf("Results=$%.2f",result);
    	getchar();
    	/* terminates */
    	return 0;
    	}
    	
    
    	double fnCurrencyConversion(int choice,double dollars,double array[])
    {
    	
    	double conversion;
    }	
    	/* display user selection */
    	switch (choice)
    	}
    	case 1: /* Australian Dollar */
    	printf("You have selected the Australian dollar.\n");
    	conversion = dollars*array[0]; /* Dollar amount is converted */
    	break;
    	
    	case 2: /* Korean WON */
    	printf("You have selected the Korean (WON).\n");
    	conversion = dollars*araay[1]; /* Dollar amount is converted */
    	break;
    	
    	case 3: /* Canadian Dollar */
    	printf("You have selected the Canadian dollar.\n");
    	conversion = dollars*array[2]; /* Dollar amount is converted */
    	break;
    	
    	case 4: /* Swiss Franc */
    	printf("You have selected the Swiss Franc.\n");
    	conversion = dollars*array[3]; /* Dollar amount is converted */
    	break;
    	
    	case 5: /* Danish Krane */
    	printf("You have selected the Danish Krane.\n");
    	conversion = dollars*array[4]; /* Dollar amount is converted */
    	break;
    	}
    	return conversion; /* returns conversion rate */
    	}
    	
    	void menu(int*C)
    	{
    
    	/* local variables for menu() */ 
    	int choice; 
    	/* show options to user */ 
    	printf("Currency Conversion\n"); /* Heading */
    	printf("This progran converts currency for a US dollar.\n"); /* Prints Topic */ 
    	printf("Enter a number to select currency.\n"); /* User enters a number that correspond to the currency he/she wnats to convert */
    	printf("[1]-Australian (AUD)\n"); 
    	printf("[2]-South Korean (WON)\n"); 
    	printf("[3]-Canadian Dollar (CAD)\n"); 
    	printf("[4]-Swiss Franc (CHF)\n"); 
    	printf("[5]-Danish Krane (DKK)\n"); 
    	choice = 0; 
    	while(choice < 1 || choice > 5) /* If correct number is not entered, returns to selection */
    	{
    		scanf("%d", &choice);  
    		if (choice > 0 && choice <= 5 ) 
    		{ 
    			/* true path */ 
    			printf("You selected a valid number, please continue.\n", choice); /*Correct number was entered */
    
    			/* conditional statement */ 
    		}
    		else
    		{ 
    			/* false path */ 
    			printf("You didnt follow my instructions!\n"); /* User did not enter the correct number */
    
    			/* Prompts the user for a number */ 
    
    			printf("Please enter a valid number number ->"); /* Return statement because of incorrect number entered */ 
    		}
    		/* set value of referenced variable */ 
    		*C = choice;
    		
    
    	}

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ok, I did a little bit of work cleaning some things up. The program will compile now, and I ran it and converted the Canadian dollar and it seems to do it ok. Did not really check it for errors if you enter an invalid number, but perhaps you can take it from here?
    Code:
    #include <stdio.h>
    
    void menu(int*C);
    double fnCurrencyConversion( int choice, double dollars, double conversion_rate[] );
    
    int main (void)
    {
    	double conversion_rate[5]={1.48655, 1.166, 1.3469, 1.371, 6.5485};
    	double dollars;
    	double result;
    	int choice;
    	
    
    
    
    	/* display menu */
    	/* call function passing by reference */
    
    	menu(&choice);
    	/* display user selection */
    	printf("Please enter dollar amount to convert:\n");
    	scanf("%1lf", &dollars);
    	
     	result=fnCurrencyConversion(choice, dollars, conversion_rate);
    	printf("Results=$%.2f\n\n", result);
    	getchar();
    	/* terminates */
    			
    	return 0;	
    }
    	
           double fnCurrencyConversion(int choice, double dollars, double conversion_rate[])
    	     {
    		  double conversion;
    	       
    		  /* display user selection */
    		  switch (choice){
            
    		  case 1: /* Australian Dollar */
    		       printf("You have selected the Australian dollar.\n");
    		       conversion = dollars*conversion_rate[0]; /* Dollar amount is converted */
    		       break;
    	
    		  case 2: /* Korean WON */
    		       printf("You have selected the Korean (WON).\n");
    		       conversion = dollars*conversion_rate[1]; /* Dollar amount is converted */
    		       break;
    	
    		  case 3: /* Canadian Dollar */
    		       printf("You have selected the Canadian dollar.\n");
    		       conversion = dollars*conversion_rate[2]; /* Dollar amount is converted */
    		       break;
    	
    		  case 4: /* Swiss Franc */
    		       printf("You have selected the Swiss Franc.\n");
    		       conversion = dollars*conversion_rate[3]; /* Dollar amount is converted */
    		       break;
    	
    		  case 5: /* Danish Krane */
    		       printf("You have selected the Danish Krane.\n");
    		       conversion = dollars*conversion_rate[4]; /* Dollar amount is converted */
    		       break;
    		  }
    		  return conversion; /* returns conversion rate */
    	     }
    	
           void menu(int*C)
                {
    
    		 /* local variables for menu() */ 
    		 int choice; 
    	
                     /* show options to user */ 
    		 printf("Currency Conversion\n"); /* Heading */
    		 printf("This progran converts currency for a US dollar.\n"); /* Prints Topic */ 
    		 printf("Enter a number to select currency.\n"); /* User enters a number that corresponds
    								  * to the currency he/she wnats to convert 
    								  */
    		 printf("[1]-Australian (AUD)\n"); 
    		 printf("[2]-South Korean (WON)\n"); 
    		 printf("[3]-Canadian Dollar (CAD)\n"); 
    		 printf("[4]-Swiss Franc (CHF)\n"); 
    		 printf("[5]-Danish Krane (DKK)\n"); 
    		 choice = 0; 
    		 
    		 while(choice < 1 || choice > 5){  /* If correct number is not entered, returns to selection */
    			scanf("%d", &choice);  
    			if (choice > 0 && choice <= 5 ){ 
    			
                              /* true path */ 
    			     printf("You selected a valid number, please continue.\n"); /*Correct number was entered */
    
    			  /* conditional statement */ 
    			}
    			else{
     
    			  /* false path */ 
    			     printf("You didnt follow my instructions!\n"); /* User did not enter the correct number */
    
    			  /* Prompts the user for a number */ 
    
    			     printf("Please enter a valid number number ->"); /* Return statement because of 
    									       * incorrect number entered 
    									       */ 
    			}
    		
                              /* set value of referenced variable */ 
    			*C = choice;
    		
    
    		 }
    	    }
    ~/
    Last edited by kermit; 10-06-2003 at 06:04 PM.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Smile Thank you

    I am greatful for your experience. You're the greatest. Thank you.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Heh...I think that normally I have proven that I am far from greatest on this board. I am happy to have been of some help to you though. Actually, I kept playing with your code, and revised it a little to make the output a little nicer looking (I thought it looked nicer anyhow.)

    edit: I am withdrawing the revised code, because it was stinky.

    You might consider also looking into using something besides scanf to get user input, as there is no provision for error checking. A search on this board should provide you with the necessary information.
    Last edited by kermit; 10-06-2003 at 07:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM