Short and simple question on C/C++.

This is a discussion on Short and simple question on C/C++. within the C Programming forums, part of the General Programming Boards category; Hello, I've got a simple question: I need something to happen, only if, within a vector, there is enough vector ...

  1. #1
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34

    Short and simple question on C/C++.

    Hello, I've got a simple question:

    I need something to happen, only if, within a vector, there is enough vector positions equal to a value/s.

    For instance, I have vector[10]

    while(4 out of 10 positions in the vector, no matter which = 1)
    {something something}

    Thanks for any help.
    Last edited by Salem; 10-08-2010 at 06:40 AM. Reason: removed tags from non-code

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    1st: What?
    2nd: do your own homework
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    1st: Was I not clear enough?
    2nd: I will ignore this part. To start trouble, I'm not the one.

    It's simple, let's say:
    vector[1] = 3, vector[2]= 5, vector[3] = 3, vector[4] = 9, vector[5] = 4

    I need that, if 2 out of the 5 vector positions have value=3, then something(printf, scanf, w/e) would happen... If it isn't clear enough I'm sorry but I can't find any other way to explain it!!

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by Pecado View Post
    1st: Was I not clear enough?
    No, you weren't. In fact I think you probably deserve an award for the most poorly posed question of all time.

    Quote Originally Posted by Pecado View Post
    2nd: I will ignore this part. To start trouble, I'm not the one.

    It's simple, let's say:
    vector[1] = 3, vector[2]= 5, vector[3] = 3, vector[4] = 9, vector[5] = 4

    I need that, if 2 out of the 5 vector positions have value=3, then something(printf, scanf, w/e) would happen... If it isn't clear enough I'm sorry but I can't find any other way to explain it!!
    As previously stated. We don't just hand out code. The whole point of this forum is to learn, which means that if you want our help you have to post an initial attempt and we will help you from there.

  5. #5
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Quote Originally Posted by Swarvy View Post
    No, you weren't. In fact I think you probably deserve an award for the most poorly posed question of all time.
    You deserve an award for the most sympathetic and charming person then. And I am being ironic.

    Code:
    As previously stated. We don't just hand out code. The whole point of this forum is to learn, which means that if you want our help you have to post an initial attempt and we will help you from there.

    I've seen many many code handouts, and I've been around for a couple of days only.

    But I get your point, and I'm not expecting you to code a whole program for me, nor did I ever intend you to. I never said "code this", did I?

    Anyways, here goes what I'm trying to do, in a "neanderthal" way:
    Code:
    if(vector[1]==2 && vector[2]==1 && vector[3]==1 && vector[4]==1)
    {printf("There are at least 3 vector positions with the value 1");}
    
    
    if(vector[1]==1 && vector[2]==2 && vector[3]==1 && vector[4]==1)
    {printf("There are at least 3 vector positions with the value 1");}
    
    
    if(vector[1]==1 && vector[2]==1 && vector[3]==2 && vector[4]==1)
    {printf("There are at least 3 vector positions with the value 1");}
    
    
    if(vector[1]==1 && vector[2]==1 && vector[3]==1 && vector[4]==2)
    {printf("There are at least 3 vector positions with the value 1");}
    Now, imagine the above, with a 40 positions vector in which I need, say 15 positions to be = 1. I'd be years to do so. So my questions is, if any of you happen to know a simpler way to do this. Thanks.
    Last edited by Pecado; 10-08-2010 at 10:07 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You need loop.
    Pseudo code:
    Code:
    count = 0;
    while( v in vector) {
      if( v == a) count++;
    }
    printf("There are at least %d vector(s) with the value %d\n",count,a);

  7. #7
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Quote Originally Posted by Bayint Naung View Post
    You need loop.
    Pseudo code:
    Code:
    count = 0;
    while( v in vector) {
      if( v == a) count++;
    }
    printf("There are at least %d vector(s) with the value %d\n",count,a);
    Couldn't figure out the "v in vector", but you gave me an idea to solve the problem. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple and short fork-join parallelization
    By cole01527 in forum C Programming
    Replies: 5
    Last Post: 07-04-2005, 04:08 AM
  2. A Simple Compiler/Linker
    By SMurf in forum C Programming
    Replies: 9
    Last Post: 02-17-2005, 03:53 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Very simple question
    By SmokingMonkey in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2003, 09:07 PM
  5. Simple Short Class Question
    By Travis Dane in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2002, 07:12 PM

Tags for this Thread


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