Thread: Arrays in C++

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Chicago
    Posts
    1

    Arrays in C++

    I am not very good at programming so thanks to all who help in advance. I can only utilize basic code that involves while, for, and do loops. in this case i think for loops are the most efficient.

    I need to have a vector given 20 integers, smallest to largest, and then remove the items in the vector in the fewest possible steps by using the following process:
    An element X in the data set can be removed along with X – 1 or X + 1 if they are present in the set. After each deletion, print the values remaining in the data set.

    heres my code so far (only have the input) Any ideas?
    Code:
    #include <stdio.h>
    
    #define numdata 20
    
    void getdata(int[]);
    void riddata(int[]);
    int main ()
    {
      int data[numdata]; //array to hold data to sort
    
      getdata(data);
      riddata(data);
      return(0);
    }
    
    void getdata(int x[])
    {
      int lcv; //loop control variable get data
    
      for(lcv = 0; lcv < numdata; lcv++)
      {
        printf("Enter data #%d: ", lcv+1);
        scanf("%d", &x[lcv]);
      }
    }
    
    void riddata(int x[])
    {
      for
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by stinky123 View Post
    I am not very good at programming so thanks to all who help in advance. I can only utilize basic code that involves while, for, and do loops. in this case i think for loops are the most efficient.
    Concerning efficiency, there isn't a measurable difference in performance between while for or do, you choose one out of convenience, and that depends on what kind of loop it is. The for loop would be the right choice here, because it's a counting loop and for groups all three parts of a counting loop together.

    I need to have a vector given 20 integers, smallest to largest, and then remove the items in the vector in the fewest possible steps by using the following process:
    An element X in the data set can be removed along with X – 1 or X + 1 if they are present in the set. After each deletion, print the values remaining in the data set.
    Well the smallest X will be encountered first if they are in fact arranged smallest to largest. If the position of X is p then if *(p+1) == X+1 AND *(p+2) == X+2, then you can call erase(p, p+3); on the vector and proceed from there. If the test fails, you'll have to make the next X, *(p+1), the new X.

    Of course, you aren't using a vector object, yet. You're using an array. With arrays, you can't really use erase. Instead, I would copy over all the Xs involved, where the above test fails, into a new array.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using scanf and printf in C++?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM