Like Tree3Likes
  • 1 Post By Click_here
  • 1 Post By rosemary
  • 1 Post By rosemary

dollars to yen.

This is a discussion on dollars to yen. within the C Programming forums, part of the General Programming Boards category; Hi all- I am learning arrays this week and I am confused how to use them. My program compiles but ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    dollars to yen.

    Hi all-
    I am learning arrays this week and I am confused how to use them. My program compiles but stops executing after I enter the "dollar amount for each." If anyone could help, it would be great. Thanks.
    Code:
    #include <stdio.h>
    
    
    const int MAX_CURRENCYS=100;
    
    
    void readDollars ( float Dollars[0], int count);
    void DollarsToYen ( float Dollars[0], float Yen[0], int count);
    void displayData ( float Dollars[0], float Yen[0], int count);
    
    
    	int main (void)
    	{
    		int count;
    		float Dollars[MAX_CURRENCYS],
    		  Yen[MAX_CURRENCYS];
    		  
    		printf("Enter the number of deposits: ");
    		scanf("%d",&count);
    	
    		readDollars(Dollars,count);
    	
    		DollarsToYen(Dollars,Yen,count);
    	
    		displayData(Dollars,Yen,count);
    
    
    		return 0;
    	}
    	void readDollars (float Dollars[0], int count )
    	{
    		int j;
    		for (j=0;j<count;j++)
    		printf("Enter the dollar amount for each: ");
    			scanf("%d", Dollars[0]);
    	}
    	void DollarsToYen (float Dollars[0], float Yen[0], int count)
    	{	
    		int j;
    		for (j=0;j<count;j++)
    			Yen[0]=Dollars[0]*102;
    	}
    	void displayData (float Dollars[0], float Yen[0], int count)
    	{
    		int j;
    		for(j=0;j<count;j++)
    		printf("\n   Dollars    Yen    \n");
    		printf("  %7f  %12f\n", j+1);
    	}

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Let's some general things first.
    Code:
    int array[10];
    This code declares an array of integers.The size of the array is ten,so ten integers can be stored there.Remember that we starting counting by zero.so the first element is
    Code:
    array[0]
    and the last element is
    Code:
    array[9]
    9 comes from 10-1 .

    In order to access the i-th element of an array and set this element equal to five i would write this
    Code:
    array[i] = 5;
    As you already know we access arrays by loops.So if i want to fill all the array with five,in other words set all elements of it equal to five i would write this
    Code:
    int i;
    for( i = 0 ; i < 10 ; i++)
    {
            array[i] = 5;
    }
    When i want to pass an array as argument to a function i would write this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void myFunction(int *array , int n);
    void print(int *array , int n);
    
    int main(void)
    {
        int n = 10;
        int array[n];
        myFunction(array,n);
        print(array,n);
        return 0;
    }
    
    void myFunction(int *array , int n)
    {
         int i;
         for( i = 0 ; i < n ; i++)
         {
              array[i] = 5;
         }
    }
    
    void print(int *array , int n)
    {
         int i;
         for( i = 0 ; i < n ; i++)
         {
              printf("%d\n",array[i]);
         }
    }
    we pass as an argument the pointer of the array.An array is a pointer to the first element of the array.Do not assume though that arrays and pointers are equivalent

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Ok. I've got it to here. Why do I get all zeros for answers? Thanks.

    Code:
    #include <stdio.h>
    #include <iomanip>
    
    
    const int MAX_S = 100;          /* Max number of Bank Deposits */
    const float DOLLARS_TO_YEN = 102.2;   /* 1 Dollar = 102.2 Yen */
    
    
    
    
    /* Function prototypes */
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    
    int main (void)
    {
    	int count;               // Actual number of  deposits */
        float Dollars[MAX_S],    /* Dollars  */
               Yen[MAX_S];      /* Yen */
    
    
        /* Prompt the user for the number of deposits. */
        printf("\n Enter the number of  deposits: ");
    	scanf("%d", &count);
    
    
        // Read the amount in each deposit .
        readDollars(Dollars,count);
    
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars,Yen,count);
    
    
        // Display the amount in each deposit
        displayData(Dollars,Yen,count);
    
    
    return 0;
    }
    
    
    /* read number of Dollars in each  account from the keyboard */
    
    
    void readDollars ( float Dollars[], int count )
    {
       int j;
       for ( j = 0; j < count; j++ )
       printf("Enter the  dollar amount for each : ");
       scanf("%f", &Dollars[j]);
    }
    /*gives number of Yen in each account */
    
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ )
    	Yen[j] = Dollars[j] * DOLLARS_TO_YEN;
    }
    
    
    /* displays the amount in each account in both Dollars and Yen */
    
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
    	for (  j = 0; j < count; j++ )
    	printf(" $%.2f, %.2f", Dollars[j], Yen[j]);
    
    
    }

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Code:
    printf("Enter the  dollar amount for each : ");
    for ( j = 0; j < count; j++ )
       scanf("%f", &Dollars[j]);
    Last edited by Click_here; 10-07-2012 at 08:10 PM. Reason: Better solution
    laserlight likes this.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    yes, thank you. i did figure that out, but my new code gives me the correct exchange for the first dollar amount only. after that, it gives zeros. also, i can't figure out how to make columns. thanks for any help.

    Code:
    #include <stdio.h>
    #include <iomanip>
    
    
    
    
    /* Function prototypes */
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    
    int main (void)
    {
    	int count;               // Actual number of  deposits */
        float Dollars[100],    /* Dollars  */
               Yen[100];      /* Yen */
    
    
        /* Prompt the user for the number of deposits. */
        printf("\n Enter the number of  deposits: ");
    	scanf("%d", &count);
    
    
        // Read the amount in each deposit .
        readDollars(Dollars,count);
    
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars,Yen,count);
    
    
        // Display the amount in each deposit
        displayData(Dollars,Yen,count);
    
    
    return 0;
    }
    
    
    /* read number of Dollars in each  account from the keyboard */
    
    
    void readDollars ( float Dollars[], int count )
    {
       int j;
       printf("Enter the  dollar amount for each : ");
       scanf("%f", &Dollars[j]);
    }
    /*gives number of Yen in each account */
    
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ )
    	Yen[j] = Dollars[j] * 102.2;
    }
    
    
    /* displays the amount in each account in both Dollars and Yen */
    
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
    	for (  j = 0; j < count; j++ )
    	printf("    $%.2f,    %.2f",      Dollars[j], Yen[j]);
    
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    You have made some effort to indent your code, and that is good, but it can still be improved, e.g.,
    Code:
    #include <stdio.h>
    #include <iomanip>
    
    /* Function prototypes */
    void readDollars( float Dollars[], int count );
    void DollarsToYen( float Dollars[], float Yen[], int count );
    void displayData( float Dollars[], float Yen[], int count );
    
    int main(void)
    {
        int count;           /* Actual number of deposits */
        float Dollars[100],  /* Dollars  */
              Yen[100];      /* Yen */
    
        /* Prompt the user for the number of deposits. */
        printf("\n Enter the number of  deposits: ");
        scanf("%d", &count);
    
        // Read the amount in each deposit .
        readDollars(Dollars, count);
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars, Yen, count);
    
        // Display the amount in each deposit
        displayData(Dollars, Yen, count);
    
        return 0;
    }
    
    /* read number of Dollars in each  account from the keyboard */
    void readDollars( float Dollars[], int count )
    {
       int j;
       printf("Enter the  dollar amount for each : ");
       scanf("%f", &Dollars[j]);
    }
    
    /*gives number of Yen in each account */
    void DollarsToYen( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ )
            Yen[j] = Dollars[j] * 102.2;
    }
    
    /* displays the amount in each account in both Dollars and Yen */
    void displayData( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ )
            printf("    $%.2f,    %.2f", Dollars[j], Yen[j]);
    }
    Notice that I also removed the many blank lines that you had. Generally, a single blank line is enough to separate groups of statements. If you want to separate functions with two or three blank lines, I guess that's okay, but generally having more than three blank lines hinders readability.

    Next, I notice that in your readDollars function, you don't have a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    thanks laserlight. i got it to work!

    Code:
    #include <stdio.h>
    #include <iomanip>
    
    
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    
    int main (void)
    {
    	int count;               
        float Dollars[100],    
               Yen[100];      
    
    
        printf("\n Enter the number of  deposits: ");
    	scanf("%d", &count);
    
    
        readDollars(Dollars,count);
    
    
        DollarsToYen(Dollars,Yen,count);
    
    
        displayData(Dollars,Yen,count);
    
    
    return 0;
    }
    void readDollars ( float Dollars[], int count )
    {
       int j;
       printf("Enter the  dollar amount for each : ");
       for ( j = 0; j < count; j++ )
       scanf("%f", &Dollars[j]);
    }
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ )
    	Yen[j] = Dollars[j] * 102.2;
    }
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
    	printf("\n     Dollars     Yen     \n");
    	for (  j = 0; j < count; j++ )
    	{
    	printf("      $%.2f,    %.2f \n",      Dollars[j], Yen[j]);
    	}
    }
    Click_here likes this.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    thanks click_here, also!
    Click_here likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yen to Dollars conversion problem
    By JJohnson in forum C++ Programming
    Replies: 12
    Last Post: 05-14-2011, 04:00 PM
  2. 200,000 dollars to get linux to run on xBox
    By cpluspluser__ in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-06-2003, 10:23 PM
  3. 240 dollars and a coke later...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-19-2002, 07:56 AM
  4. Someone grab me those dollars
    By Mario in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 07:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21