GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2008, 17:52
Krisis Krisis is offline
New Member
 
Join Date: Oct 2008
Posts: 2
Krisis is an unknown quantity at this point

Adding Arrays


I am having trouble with a current C++ homework problem. I have to fill up 2 partially filled arrays with 2 numbers that are of 20 digits or less. I have to read the numbers as a character and put each individual array block with each digit. Once both arrays are filled as chars i have to convert the digits from chars to ints and then add the 2 arrays. If the sum of the 2 has more than 20 digits, then i have to state an error msg that it is overwhleming. Any help here would be great.

Here is what i have so far:

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 20;

int fill_array(int num1[], int SIZE);
int fill_array2(int num2[], int sum, int SIZE);
int add_arrays(int num1[], int num2[], int sum, int SIZE);

int fill_array(int num1[], int SIZE)
{
  int next;
  cout << "Enter a number with up to 20 digits followed by a period: ";
  cin >> next;
  int i;
  for (i=0; i<SIZE; i++)
    {
      cin >> next;
      if(next == '.')
	{
	  break;
	}
      num1[i]=next;
    }
  return i;
}

int fill_array2(int num2[], int SIZE)
{
  int nextt;
  cout << "Enter another number with up to 20 digits followed by a period: ";
  cin >> nextt;
  int j;
  for (j=0; j<SIZE; j++)
    {
      cin >> nextt;
      if(nextt == '.')
	{
	  break;
	}
      num2[j]=nextt;
    }
  return j;
}

int main()
{
  int num1[SIZE], num2[SIZE];
  fill_array(num1, SIZE);
  fill_array2(num2, SIZE);
  return 0;
}
Last edited by LuciWiz : 05-Nov-2008 at 00:53. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 08-Nov-2008, 02:58
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Adding Arrays


Ok, so you want to store the numbers as char arrays and add them. So why are you storing the numbers in an integer array in your program?
  #3  
Old 08-Nov-2008, 12:12
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Adding Arrays


Yes as noop said, it sounds like your supposed to convert a char string ot 'digits' to a numeric value.
Good excercise. So you will want to work with arrays of type char. Does this make sense to you?:
CPP / C++ / C Code:
#include <cstdio>
int main()
{
  char  ns1[20]=                 {'4','5','8','9', '\0'},
        ns2[20]= {'4','5','8','9','2','6','3','5', '\0'};
        /* expect: 4   5   8   9   7   2   2   4       */    
   int num1= 0, num2= 0;

  num1 = chars_to_int(ns1);  /* design your function to fill this requirement */
  /* etc ...  */
  return 0;
}
Are you familiar with what values the ascii 'digits' characters 0 thru 9 are? ...(look up on a chart)
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Arrays as function arguments - return arrays from functions pisuke C Programming Language 2 25-Jul-2007 12:03
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 16:54
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.