Array equal to array

This is a discussion on Array equal to array within the C Programming forums, part of the General Programming Boards category; Hey everybody, I know I am doing this wrong but I forgot the right way to do this... Code: answer[answerplace] ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    Array equal to array

    Hey everybody, I know I am doing this wrong but I forgot the right way to do this...
    Code:
    answer[answerplace] = question[find];
    this is in a loop that keeps repeating until a character is found. But I am
    doing
    Code:
    answerplace++;
    while I am doing
    Code:
    find++;
    thinking it will put the place from question into the place from answer, this doesn't work but I know this is done another way that I forgot.

    Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know what it is you're trying to do, but your explanation is terrible. Your first line just assigns a value from one array to another. It doesn't compare anything. If you aren't trying to compare, then just do:
    Code:
    for( x = 0; x < size; x++ )
        array1[ x ] = array[ x ];
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    I was just wondering it I could make do something like

    Code:
    answer[answerplace] = question[find];
    and while "answerplace" and "find" equal two different numbers. I keep on getting a segmentation fault for it.

    answer and question are chars.
    Thanks.

    EDIT: I am trying to make answer equal to question.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    Did you make sure you aren't getting out of bounds on an array?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    Quote Originally Posted by xniinja View Post
    I was just wondering it I could make do something like

    Code:
    answer[answerplace] = question[find];
    and while "answerplace" and "find" equal two different numbers. I keep on getting a segmentation fault for it.

    answer and question are chars.
    Thanks.
    Simple - just make sure you don't exceed the bounds of either array...
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    when I initiate them I do

    Code:
    char question[find];
    //and
    char answer[answerplace];
    but when I initiate answerplace I make it equal to 0 should I make it 20 or something.

  7. #7
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    oh it worked thanks guys.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    Quote Originally Posted by xniinja View Post
    when I initiate them I do

    Code:
    char question[find];
    //and
    char answer[answerplace];
    but when I initiate answerplace I make it equal to 0 should I make it 20 or something.
    Um...why would you declare a zero-length array? Maybe you should post a working program to demonstrate more clearly how you are doing things.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    The program is attached. It has some glitches. Like when you enter a question with a space in the test maker it will skip over the answer and go back to question (you'll see). This program (if you fix it) can be used to study.

    Can you tell me about some glitches you find, thanks.

    EDIT: i also initiate some values midcode.
    Attached Files Attached Files

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    I don't know who is teaching you C but you should inform them they are doing a terrible job.

    This is always true: while( loopkey == 'y' || '\n' ) Because even if loopkey is not 'y', '\n' is a non-zero (true) value. Try while( loopkey == 'y' || loopkey == '\n' ) Similarly you do stuff like while( looper != looping ) which is fairly redundant and easy to confuse people by. Should looper change or looping change to stop the loop? Neither is a constant....

    You don't really understand too much about functions either.

    Code:
    type fun ( type paramA, type paramB );
    
    type fun ( type paramA, type paramB )
    {
       ... code ...
       return [a type variable];
    }
    Functions have many parts, all of which are highlighted: the return type, the function name, the different types of parameters, the parameter names, and braces. Each part is arranged in the manner that you see.

    The first part is called a function prototype. These statements typically live in #included header files (.h files), and are there to inform the compiler of a particular function's existence. It is so the compiler can statically check types of arguments (the things passed into a function) against the corresponding parameter (what the function calls the argument) types. These are also handy, since you can now define the function at any time, such as after main() or in a .c file.

    As for the flash card program... I'm still not very clear on what you mean to ask. Flash cards have two sides, a clue side and an answer side... I can't think of a time when you should make both sides the same thing through assignment. It's always a mistake to do that.

    If you mean to compare a lot of the end user's answers to the actual flash cards you could compare with the equality symbol, in a loop:

    user[x] == answer[x]

    Again though, I would think the question side of the flash card is only used to show him the question. The clue part of the flash card is not going to be helpful in deciding whether he's right or wrong.

  11. #11
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    That would be myself for I am on 15. And my school doesn't have any programming classes. Thank you.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xniinja View Post
    That would be myself for I am on 15. And my school doesn't have any programming classes. Thank you.
    Ok what are you using for text books?

    You should do all the examples in the books... but don't just compile them and go "cool"...
    make changes, do things to make them stop working. You need to know what doesn't work
    just as much as you need to know what does.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Also noticed this


    Code:
    while(questionloop = 'y')
    That is not going to compare if the questionloop hold 'y'. Insided it going to aassign questionloop with the value 'y' overriding whatever the questionloop already had. You will have careflul with these things, can get worse when working a huge projects.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  14. #14
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    Quote Originally Posted by CommonTater View Post
    Ok what are you using for text books?

    You should do all the examples in the books... but don't just compile them and go "cool"...
    make changes, do things to make them stop working. You need to know what doesn't work
    just as much as you need to know what does.
    Its just a hobby... Well it didn't start as a hobby. My dad threw me into it when he wanted a calender program (it got half done). I "worked" for him last summer. And all the code I write isn't from a book its all from scratch, I don't even have a book (probably a good investment though) I started last summer by watching Youtube videos. So yea... this isn't some stuff I just copy out off a book and ask help on it. Also thanks for the tip, I appreciate it.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xniinja View Post
    Its just a hobby... Well it didn't start as a hobby. My dad threw me into it when he wanted a calender program (it got half done). I "worked" for him last summer. And all the code I write isn't from a book its all from scratch, I don't even have a book (probably a good investment though) I started last summer by watching Youtube videos. So yea... this isn't some stuff I just copy out off a book and ask help on it. Also thanks for the tip, I appreciate it.
    No cost to you....

    Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy
    Teach Yourself C in 21 Days -- Table of Contents
    C Programming
    C tutorials

    The problem with videos is they put things before your eyes, but not between your ears!
    There's just nothing like the printed word for making stuff sink in.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-27-2009, 10:04 PM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  5. Trouble making an Array equal a Function
    By Ozor in forum C++ Programming
    Replies: 16
    Last Post: 07-11-2003, 10:33 AM

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