Thread: C++ program question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    C++ program question

    1.
    int array_a[5] = { 3, -5, 6, -2, 1}
    write a program using a loop statement to double the value of every time in array_a and then display only negative items in the array. use the statement cout for output.

    2.
    An array with a list of numbers {89, 68, 5, 12} is to be sorted in ascending order using the selection sort algorithm. There should be 3 rounds of elements swapping before the list is sorted. Show the array contents after each round of swapping in sequence.


    can anybody help with me with these questions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The way homework/assignments work here is that you show that you are working on it, then we help you get to a solution, not "You post the assignment, we solve it for you".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We can certainly help you with problems you come across in the process when you get underway, however there's nothing for us to help you with yet because you don't appear to have started.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or maybe rather... you haven't asked any really specific questions. Sometimes one might have trouble getting started. Then you could ask for some advice from whatever you might have pondered about yourself.
    But do try getting started and asking when/if you get stuck. That's the best way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    here is specofoc questions..
    for the q1.
    for doubleing...
    Code:
    const int arraysize = 5;
    int array_a[ arraysize] = { 3, -5, 6, -2, 1};
    for ( int k =0; k < arraysize; k++)
     [k] *= 2;
    then, how can i display the negative numbers only?

    for q2
    is the each round of array contents :
    1st : 68,89,5,2
    2nd : 5,68,89,2
    3rd: 2,5,68,89

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's not quite right. That can't be your real code, can it?
    But as for only negative numbers, you only need to loop through your array and check if the number is negative, THEN print it, otherwise don't.
    You shouldn't have much problem with that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    For the second question, consider using pointer notation via selection sort. I do not know if you have covered pointers yet, but somthing like this:

    Code:
    void swapEelements ( int *pElement1, int *pElement2 )
    {
       int hold = *pElement1;
       
       *pEelement1 = *pElement2;
    
       *pEelement2 = hold;
    }
    Of course, there is a load more to this method, but that sort of algorithm is quite efficient, although you do have to pass each element to the function to swap it, and this could be a little complex. Its just an idea.
    Double Helix STL

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For the second question, consider using pointer notation via selection sort. I do not know if you have covered pointers yet, but somthing like this:
    Or rather, pass by reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Or rather, pass by reference.
    thanks..i get the q2 by selection sort of pass by reference usign pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM