Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Arrays in C++

Name: Anonymous 2012-11-12 21:48

So lets say I have an array, int array[3] = {123, 456, 789}.
If I wanted to take the first number of each value of the array and store it in another array, say int first[3];, how would I do that? I tried using a pointer to read the first memory address of each value of the array but it keeps returning the full array.

Name: Anonymous 2012-11-12 22:01

you are a faggot

Name: Anonymous 2012-11-12 22:02

I think what you want, is an array {1, 4, 7}? If so, you'll have to do some shit with the modulus operator. %

So you'd say for this shit

int first[3] = {0, 0, 0};
for(int i = 0; i < 3; i++) {
    first[i] = array[i] % 100;
}

Name: Anonymous 2012-11-12 22:03

array.each {|e| p e.to_s[0].chr}
array.each {|e| p((e.to_s[0])-?0)}

Name: Jew 2012-11-12 22:03

You need to convert the integer to a string first

Name: Anonymous 2012-11-12 22:03

Fuck I'm wrong. Don't use %. Just use division. So 123 / 100 = 1.
Int division for you.

Name: Anonymous 2012-11-12 22:05

>>3
Do you need RSpec?

Name: Anonymous 2012-11-12 22:07

>>6
Yah but what if he has a 1 in there or something like that, then he'll get 100 which isn't correct

Name: Anonymous 2012-11-12 22:08

>>6
No, dammit!
n / 10⌊ln(n)⌋

Name: Anonymous 2012-11-12 22:09

Just convert the number to a string then get the first char in the string, guaranteed to work every time

Name: Anonymous 2012-11-12 22:11

Niggers are black.

Name: Anonymous 2012-11-12 22:36


(map car array)

Name: Anonymous 2012-11-12 22:38


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void getFirstNumbers
(
  int * dest,
  int destLength,
  int * src,
  int srcLength
)
{
  assert(srcLength == destLength);

  //char t[10];

  for(int * pArray = src, * pFirst = dest;
      pArray != src + srcLength;
      ++pArray, ++pFirst)
  {
    /*
     *  LEAH quality
     *
     *  t = itoa(*pArray, t, 10);
     *  *pFirst = atoi(t[0]);
     *
     */

    *pFirst = *pArray;
    for( ; *pFirst > 10; *pFirst /= 10);
  }
}

int main()
{
  int array[3] = {123, 456, 789};
  int first[3];

  getFirstNumbers(first, 3, array, 3);
 
  for(int * pFirst = first;
    pFirst != first + 3;
    ++pFirst)
  {
    printf("%d\n", *pFirst);
  }

  return 0;
}

Name: Anonymous 2012-11-12 22:55

smoke weed array

Name: Anonymous 2012-11-12 23:08

>>13
lol no.
for (int i : array) {
  int n = 1;
  while (i/n > 0) n *= 10;
  std::cout << n;
}

Name: Anonymous 2012-11-12 23:17

|:&.(10&#.^:_1)

Name: Anonymous 2012-11-12 23:20

>>15

sepples is not kawaii

pls go

Name: Anonymous 2012-11-13 0:22

needs more <algorithm>



#include <algorithm>

int main(void)
{
    int numbers[] {1,2,3,4,5,6,7,8,9,0,12,34,56,78,90,123,456,789};
    int first[sizeof(numbers)/sizeof(*numbers)] {0};
    auto ptr = first;
    auto get_first = [&ptr](int val) { unsigned int n = 10;
                                       while (val/n) n*=10;
                                       *ptr++ = val/(n/10); };
    std::for_each(std::begin(array), std::end(array), get_first);
   
    printf("digits are: \n");
    for(auto digit : first) printf("%d ", digit);

    return 0;
}

Name: Anonymous 2012-11-13 1:30

var array = [123, 456, 789];
var first = array.map(function(number) {
  number = (number < 0) ? -number : number;
  return Number.prototype.toString.call(number).charAt(0);
});

Name: Anonymous 2012-11-13 1:44


(define (first lis)
  (map (compose (curry (flip -) (char->integer #\0))
                char->integer
                (curry (flip string-ref) 0)
                number->string
                abs)
       lis))

Name: Anonymous 2012-11-13 1:47

>>20
what the fuck

Name: Anonymous 2012-11-13 1:54

Now optimized with Sympta ? syntax!

(defmacro (! . body)
  `(lambda (?) ,@body))

(define (first lis)
  (map (compose (! (- ? (char->integer #\0)))
                char->integer
                (! (string-ref ? 0))
                number->string
                abs)
       lis))

Don't change these.
Name: Email:
Entire Thread Thread List