943,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1895
  • C++ RSS
Apr 27th, 2005
0

need to run these programs

Expand Post »
hi i am new to this forum and my major is computer science...this is the best thing i found on the web yet....i will try to help you out anyway i can.....i need help on this program...thanks




1. create a dynamicarray to hold the first 10 odd intergers. now create the array to triple its size to hold the first 30 odd integers.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rohitmc is offline Offline
4 posts
since Apr 2005
Apr 27th, 2005
0

Re: need to run these programs

So, what have you done so far? Do you not understand the assignment?

Hint: use a for loop, the modulus operator with the number two to find if it is even or odd
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
Apr 27th, 2005
0

Re: need to run these programs

For arrays read here:-
http://www.cprogramming.com/tutorial/lesson8.html

For modulus operator, read here:-
http://www.cprogramming.com/tutorial/modulus.html

You'll be needing for loops also. So read here:-
http://www.cprogramming.com/tutorial/lesson3.html

I suggest you declare a global variable call SIZE (and equate it to 10).
And use the SIZE in your array like this:-
array [SIZE];
and in your for loop also. Like this:-
for (int i=0; i<SIZE; i++)
{
:
do your odd number generator here
and equate it into your dynamic array
:
}

For the second part of the question, just equate SIZE (your global variable) to 30.

Hope this helps.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Pikachu is offline Offline
19 posts
since Aug 2004
Apr 29th, 2005
0

Re: need to run these programs

ok i will do this...and post here...and if there's any error pls help me out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rohitmc is offline Offline
4 posts
since Apr 2005
Apr 29th, 2005
0

Re: need to run these programs

ok here we go.....ERRORS...

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. int num_cells=10;
  7. int * a;
  8. a * new int[num_cells];
  9. for (int i=0; i<num_cells; i++)
  10. a(i)=30*1+1;
  11. cout<<"a:";
  12. for (i=0; i<num_cells; i++)
  13. int=b;
  14. b=new int [3*num_cells];
  15. cout<<a(i)<<
  16. cout <<endl;
  17. for(i=0; i<num_cells; i++)
  18. b (i) = a(i);
  19. delete [] a;
  20. num_cells *=3;
  21. a=b;
  22. b=nul;
  23. cout << "Size of array has tripled\n";
  24. for (i=num_cells; i<num_cells; i++)
  25. a(i>=10*i+1);
  26. cout<<"a:";
  27. for (i=0; i<num_cells; i++)
  28. cout << a [1]<<"";
  29. cout<<endl;
  30. delete [] a;
  31. return 0;
  32. }
<< moderator edit: added [code][/code] tags >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rohitmc is offline Offline
4 posts
since Apr 2005
Apr 29th, 2005
0

Re: need to run these programs

Arrays are indexed a[i], not a(i).
a * new int[num_cells];
Typo?
a = new int[num_cells];
C++ Syntax (Toggle Plain Text)
  1. int=b;
Did you mean i instead of int?
C++ Syntax (Toggle Plain Text)
  1. b=nul;
b? nul?

Learn to use whitespace to make your code easier to read.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 30th, 2005
0

Re: need to run these programs

Well, your codes are a little misleading. I don't quite understand what you are doing. Anyway, I'll go step by step with you. First, make sure you have this in mind:- A 10 block array like this [] [] [] [] [] [] [] [] [] []

C++ Syntax (Toggle Plain Text)
  1. for (int i=0; i<num_cells; i++)
  2. a[i]=30*1+1;
  3. cout<<"a:";
Here, I can only interpret one thing. The array will be filled with number 31 like this:- [31] [31] [31] [31] [31] [31] [31] [31] [31] [31]
But on screen you'll only see a: as you didn't include the output into your loop

Then, I see that you transfer the array a to array b. Which I do not see the need for this. But please correct me if I'm wrong
C++ Syntax (Toggle Plain Text)
  1. for(i=0; i<num_cells; i++)
  2. b [i] = a[i];


C++ Syntax (Toggle Plain Text)
  1. for (i=0; i<num_cells; i++)
  2. cout << a [1]<<"";

If everything works well, you'll only get something like this a [1]a [1]a [1]a [1]a [1]a [1]a [1]a [1]a [1]a [1]

The rest of the problem, I believe Dave had already mention them.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Pikachu is offline Offline
19 posts
since Aug 2004
May 6th, 2005
0

Re: need to run these programs

thanku for all the help...i will give another shot and post it here....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rohitmc is offline Offline
4 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HELP me as soon as possible ..!
Next Thread in C++ Forum Timeline: Help. This is a strange problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC