Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-04-2008, 05:50 PM
arctic_blizzard arctic_blizzard is offline
Newbie
 
Join Date: Oct 2008
Posts: 9
Rep Power: 0
arctic_blizzard is on a distinguished road
Default Array troubles

Hi,
I am having trouble with one of my assignments dealing with arrays. The program requires a conversion between dollars and yen for a max number of 100 deposits.

The program needs to place user input ($) into an array, then convert those $ into Yen, and then display the value of each deposit in both dollars and yen.

I have completed the program basics, but am running into difficulties on several spots.

1). I get an error "invalid operands to binary" in my conversion sub-routine
2). I cannot figure out how to display the data needed.
3). I am sure there a few other things that I am missing - any help would be great!

Code:
#include <stdio.h>
const int MAX_CURRENCYS = 100;
const float DOLLARS_TO_YEN = 102.2;

    int nums;
    float Dollars;
    float Yen;

void readDollars ( float Dollars[], int count );
void DollarsToYen ( float Dollars[], float Yen[], int count );
void displayData ( float Dollars[], float Yen[], int count );
int main()
{        
    float Dollars[MAX_CURRENCYS],
           Yen[MAX_CURRENCYS];      

    int count;
    printf("\n Enter the number of Deposits: ");
    	scanf("%d", &count);

    readDollars (float Dollars[], int count);
    
    DollarsToYen (float Dollars[], float Yen[], int count);
    
    displayData (float Dollars[], float Yen[], int count);
    
    system("pause");

}

void readDollars (float Dollars[], int count)
{
   int j;
   printf("Enter the  dollar amount for each deposit: ");
   for ( j = 0; j < count; j++ )
       scanf("%d", &Dollars);
}
void DollarsToYen ( float Dollars[], float Yen[], int count )
{
    int j;
    for ( j = 0; j < count; j++ )
    Yen=(float)(Dollars*DOLLARS_TO_YEN);

}
void displayData ( float Dollars[], float Yen[], int count )
{
    int j;
    printf("\nDollars\tYen\n");
    for (j=0; j<count; j++)

    {
        printf("    %7f     %12f \n", j + 1);
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-04-2008, 07:32 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: Array troubles

You're calling standard functions correctly, but for your own you're trying to be too tricky by half. You also need to realize what an array is: it is not a single item, but a collection of them; when looping, use the single items in the collection.
Code:
#include <stdio.h>
#include <stdlib.h> /* for system */

const int MAX_CURRENCYS = 100;
const float DOLLARS_TO_YEN = 102.2;

    int nums;
    float Dollars;
    float Yen;

void readDollars ( float Dollars[], int count );
void DollarsToYen ( float Dollars[], float Yen[], int count );
void displayData ( float Dollars[], float Yen[], int count );
int main()
{
    float Dollars[MAX_CURRENCYS],
           Yen[MAX_CURRENCYS];

    int count;
    printf("\n Enter the number of Deposits: ");
    	scanf("%d", &count);

    readDollars (Dollars, count);

    DollarsToYen (Dollars, Yen, count);

    displayData (Dollars, Yen, count);

    system("pause");

}

void readDollars (float Dollars[], int count)
{
   int j;
   printf("Enter the  dollar amount for each deposit: ");
   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]=(float)(Dollars[j]*DOLLARS_TO_YEN);

}
void displayData ( float Dollars[], float Yen[], int count )
{
    int j;
    printf("\nDollars\tYen\n");
    for (j=0; j<count; j++)

    {
        printf("    %7f     %12f \n", Dollars[j], Yen[j]);
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-04-2008, 07:33 PM
Steve.L's Avatar   
Steve.L Steve.L is offline
Programming Expert
 
Join Date: Sep 2008
Location: Ottawa,ON
Age: 19
Posts: 421
Rep Power: 3
Steve.L will become famous soon enough
Send a message via MSN to Steve.L
Default Re: Array troubles

Woah. Interesting bit of code there... I recommend going over your C++ basics, because there are many silly mistakes in that code.

I think the easiest way to do this would be to change your DollarsToYen() function. The only parameter it should take is a pointer to your array (pass-by-reference to modify it) and iterate through each value, converting it to yen.

Also, when you call your function in main(), you shouldn't be initializing variables.

EDIT: Or just copy dcs' implementation, and hopefully learn something.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-04-2008, 08:47 PM
arctic_blizzard arctic_blizzard is offline
Newbie
 
Join Date: Oct 2008
Posts: 9
Rep Power: 0
arctic_blizzard is on a distinguished road
Default Re: Array troubles

Thanks so much for the help.

I guess I thought arrays in C were treated a lot like those in Matlab (where you can manipulate entire arrays)

The whole item by item though makes sense now, Thanks again

~AB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-04-2008, 08:53 PM
Steve.L's Avatar   
Steve.L Steve.L is offline
Programming Expert
 
Join Date: Sep 2008
Location: Ottawa,ON
Age: 19
Posts: 421
Rep Power: 3
Steve.L will become famous soon enough
Send a message via MSN to Steve.L
Default Re: Array troubles

Don't try to learn to code off of Matlab, that particular piece of software is geared towards engineering and mathematics, rather than application development.

Hopefully everything is cleared up now. Arrays are generally the same in all languages (fixed length, use [x] to index them, etc...). Python is an exception, as it doesn't even implement arrays, it simply uses lists (which use arrays under the hood, but you probably won't ever see that lol).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-04-2008, 09:33 PM
arctic_blizzard arctic_blizzard is offline
Newbie
 
Join Date: Oct 2008
Posts: 9
Rep Power: 0
arctic_blizzard is on a distinguished road
Default Re: Array troubles

lol - I was an engineering student for 2 years, so Matlab was the only coding I knew (other than a little html).

I have recently switched to an MIS program, and C was brand new to me

A quick question about Python: I have heard from several people that it is usually used as a direct replacement of C - should Python be on the top of my list for programming languages to learn?

I am currently learning C, VB, and will start Java next semester.

Thanks,

~AB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-04-2008, 10:11 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is offline
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 83
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: Array troubles

**** VB, it is a waste of time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-05-2008, 02:00 AM
Steve.L's Avatar   
Steve.L Steve.L is offline
Programming Expert
 
Join Date: Sep 2008
Location: Ottawa,ON
Age: 19
Posts: 421
Rep Power: 3
Steve.L will become famous soon enough
Send a message via MSN to Steve.L
Default Re: Array troubles

Quote:
Originally Posted by arctic_blizzard View Post
lol - I was an engineering student for 2 years, so Matlab was the only coding I knew (other than a little html).

I have recently switched to an MIS program, and C was brand new to me

A quick question about Python: I have heard from several people that it is usually used as a direct replacement of C - should Python be on the top of my list for programming languages to learn?

I am currently learning C, VB, and will start Java next semester.

Thanks,

~AB
LOL. American engineering program I suppose.

Python need not be on the top of your list, I'd put C++/C and Java before it. When Py3k is officially released, I'd look into it if you get time. VB is pretty much a waste of time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-05-2008, 02:51 AM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Array troubles

For something like this I'd really recommend using C++. Use C if you need fast or low-level code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-05-2008, 09:24 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Array troubles

All of the above will depend on the type of engineering you are doing. Python is good for scripting within an OS, but not good for direct hardware interaction. C/C++/ASM are good for working directly with the hardware. VB is almost useless. Java has it's place, but probably not in your environment.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D Python Array - "List Indices Must Be Integers" (cannot access an array element) Bertsche Python 3 07-07-2008 11:51 AM
filling an array with random #s gaylo565 C# Programming 5 05-02-2008 12:21 PM
Array manipulation Question in C++ JJJIrish05 C and C++ 1 03-08-2008 03:06 PM
Usage of array structures to increment letter instances of text Yuriy M C and C++ 2 09-13-2007 11:49 AM
Python 2D array question annannienann Python 3 04-23-2007 05:36 PM


All times are GMT -5. The time now is 02:04 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads