C++ program question

This is a discussion on C++ program question within the C++ Programming forums, part of the General Programming Boards category; 1. int array_a[5] = { 3, -5, 6, -2, 1} write a program using a loop statement to double the ...

  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,304
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    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,475
    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.
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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.
    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

  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, 09: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, 06:55 AM

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