Thread: Comparing Arrays question

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    okay i feel like such a newb why the -1 in the for loops?
    for (count = 0; count<=size-1; count++)
    	{
    		cin>> your_answers[count];
    	    
    	}
    
    and also he told us to use 21 because we have to include the null terminator or some bull........

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    Here is some updated code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {       
    	int count, correct = 0;          
    	const int size = 20;
    	char your_answers[size];
    	char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};
    	cout<< " Please enter the students answers. \n" ;
    	for (count = 0; count<=size-1; count++)
    	{
    		cin >> your_answers[count];
    
    		while(true)
    		{
    			if (your_answers[count] == 'a' || your_answers[count] == 'b' || your_answers[count] == 'c' || your_answers[count] == 'd')
    				break; //Break out of while loop.
    			else
    			{
    				cout << "(" << your_answers[count] << ") Your answer is not an appropriate one. Please try again." << endl;
    				cin >> your_answers[count];
    			}
    		}
    	}
    
    	for (count=0; count<=size-1;count++)
    	{
    		if (answers[count] == your_answers[count])
    				   correct += 1; //If they got this one right, then add one to 'correct'.
    	}
    
    	if(correct >= 15) //They got 15 right
    		 cout << "\nYou passed the test. Congratulations." << endl;
    	else
    		 cout << "\nYou're a failure." << endl;
    
    	cout << "You got " << correct << " out of 20 correct." << endl;
    
    	cout << endl; //Make a new line.
    
    	for (count = 0; count<=size-1; count++)
    	{
    		cout << "Correct Answer: " << answers[count] << " | Your answer: " << your_answers[count];
    		if (answers[count] == your_answers[count]) //It's correct;
    			cout << " [CORRECT]." << endl;
    		else
    			cout << " [WRONG]." << endl;
    	}
    
    	system("PAUSE");
    
    return 0;
    }

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    Quote Originally Posted by taugust7 View Post
    Code:
    okay i feel like such a newb why the -1 in the for loops?
    for (count = 0; count<=size-1; count++)
    	{
    		cin>> your_answers[count];
    	    
    	}
    
    and also he told us to use 21 because we have to include the null terminator or some bull........
    It's -1 because arrays start at 0. So from 0 - 19 is 20. So 20 - 1 is 19.

  4. #19
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {       
      
    
    	int count, correct = 0, incorrect = 0;          
    	const int size = 20;
    	char your_answers[size];
    	char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};
    	cout<< " Please enter the students answers. \n" ;
    	for (count = 0; count<=size-1; count++)
    	{
    		cin>> your_answers[count];
    	    
    	}
    
    	for (count=0; count<=size-1;count++)
    	{
    		if (answers[count] == your_answers[count])
    				   correct += 1; 
    	}
    	for (count=0; count<=size-1;count++)
    	{ 
            if (answers[count] != your_answers[count])
    	                incorrect += 1;
        }
    
    	if(correct >= 15) 
    		 cout << "You passed the test. Congratulations. " << correct << " out of 20 correct. " << endl;
    	else
    		 cout << "You're a failure." << endl;
    cout<< "You got "<< incorrect<< " answers wrong \n";
    	
    system ("pause");
    return 0;
    }
    
    
    
    
    
    
    
    
    
    
    okay so this is what i got so far, i need to find a way to show which answers i got wrong, i.e. if number[1] is wrong display number[1] of array
    
    and i still cant figure out that damned validation

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    this is good, but i cant use (true)
    im limited in the crap i can use so if there is another simpler way of validation i would appreciate if you could help me

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Flaug, you should give pointers, not complete homework assignments.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  7. #22
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    its not a homework assignment,
    my teacher expects us to learn loops in a week and didnt cover do's and fors,
    im goin over a few assignments and tryin to get them but i need alot of help
    he told us to learn do's for's while's, so im learning whats in those chapters
    i also appreciate all the help man thank you so much

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hogwash. It's homework.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    well either way sebastianinininnininininininininininininiininininiininiiinininin
    do you know what i should look into if i want to validate my answers without using (true)
    
    all i can think of is
    if (your_answers != 'a' ||' b' ||'c'||'d')
    {
     cout<< "incorrect answer, please enter a,b,c, or d. \n"
    cin>> your_answers[count]
    }

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> do you know what i should look into if i want to validate my answers without using (true)

    Well, considering that the code you posted is both logically broken and won't even compile, I'd suggest a decent book.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  11. #26
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {       
      
    
    	int count, correct = 0, incorrect = 0;          
    	const int size = 20;
    	char your_answers[size];
    	char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};
    	cout<< " Please enter the students answers. \n" ;
    	for (count = 0; count<=size-1; count++)
    	{
    		cin>> your_answers[count]; 	    
    	}
    for (count=0; count<=size-1;count++)
    	{
    		if (answers[count] == your_answers[count])
    				   correct += 1; 
    	}
    for (count=0; count<size;count++)
        {
                   if (answers[count] != your_answers[count])
        	incorrect += 1;
        }
        
    if(correct >= 15) 
    		 cout << "You passed the test. Congratulations." << endl;
    	else
    		 cout << "You're a failure." << endl;
    	cout << "You got " << correct << " out of 20 correct." << endl;
    	cout<< " You got " << incorrect<< " out of 20 wrong. "<<endl;
    	cout << endl; 
    	for (count = 0; count<=size-1; count++)
    	{
    		cout << "Correct Answer: " << answers[count] << " | Your answer: " << your_answers[count];
    		if (answers[count] == your_answers[count]) 
    			cout << " [CORRECT]." << endl;
    		else
    			cout << " [WRONG]." << endl;
    	}
    
    
    
    
    
    
    	
    	
    system ("pause");
    return 0;
    }
    
    
    and my output is
    
     Please enter the students answers.
    bdaacabacdbcdadccbdd
    You passed the test. Congratulations.
    You got 19 out of 20 correct.
     You got 1 out of 20 wrong.
    
    Correct Answer: b | Your answer: b [CORRECT].
    Correct Answer: d | Your answer: d [CORRECT].
    Correct Answer: a | Your answer: a [CORRECT].
    Correct Answer: a | Your answer: a [CORRECT].
    Correct Answer: c | Your answer: c [CORRECT].
    Correct Answer: a | Your answer: a [CORRECT].
    Correct Answer: b | Your answer: b [CORRECT].
    Correct Answer: a | Your answer: a [CORRECT].
    Correct Answer: c | Your answer: c [CORRECT].
    Correct Answer: d | Your answer: d [CORRECT].
    Correct Answer: b | Your answer: b [CORRECT].
    Correct Answer: c | Your answer: c [CORRECT].
    Correct Answer: d | Your answer: d [CORRECT].
    Correct Answer: a | Your answer: a [CORRECT].
    Correct Answer: d | Your answer: d [CORRECT].
    Correct Answer: c | Your answer: c [CORRECT].
    Correct Answer: c | Your answer: c [CORRECT].
    Correct Answer: b | Your answer: b [CORRECT].
    Correct Answer: d | Your answer: d [CORRECT].
    Correct Answer: a | Your answer: d [WRONG].
    Press any key to continue . . .
    
    
    
    now i still need good output validation an i dont know how to do it

  12. #27
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
     while (your_answers != 'a', 'b', 'c', 'd')
            {
                  cout<< "Your answer of "<< your_answers[count]<<" is invalid, please select A,B,C or D. \n";
                  cin>> your_answers[count];
                  }
    
    i need something along these lines but i cant figure it out

  13. #28
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I don't see a shred of code that shows you *trying* to validate the input. Perhaps a combination of tolower() and isalpha() might be useful?


    Also, this sort of thing won't work:

    >> if (X != 'a' ||'b')

    Since it's identical to:

    if (X != 'a' ||'b' != 0)

    You'll need to compare each value with X (but using && instead of ||, obviously).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  14. #29
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    thank you sir ill see what i come up with

  15. #30
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    also i dont know what the crap isalpha and tolower are, this is an intro level class and im tryin to teach myself arrays and loops ya know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question about arrays
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 11-08-2006, 02:09 AM
  3. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM