Solved

Array

Posted on 2004-04-09
8
244 Views
Last Modified: 2010-04-15
What does data [5*i%6] equal?  When i equals 0, 1, 2, 3, 4, 5, and so on...

I'm somewhat confused on how the % works.
0
Comment
Question by:GJMill79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 3

Accepted Solution

by:
idt earned 50 total points
ID: 10795180
% is the modulus operator..

or simply
(5 times i) divided by 6 has a remained of what?

5 % 2 is 1

Daniel
0
 
LVL 3

Expert Comment

by:idt
ID: 10795206
#include <iostream>
int main (void) {
  for(int i=1;i<12;i++)
    std::cout << "5 * " << i << " % 6 = " << (5*i%6) << std::endl;
  return 0;
}

5 * 1 % 6 = 5
5 * 2 % 6 = 4
5 * 3 % 6 = 3
5 * 4 % 6 = 2
5 * 5 % 6 = 1
5 * 6 % 6 = 0
5 * 7 % 6 = 5
5 * 8 % 6 = 4
5 * 9 % 6 = 3
5 * 10 % 6 = 2
5 * 11 % 6 = 1

Daniel
0
 

Author Comment

by:GJMill79
ID: 10795224
I'm confused...so (5 x 1)/6   ? = 1 ???  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GJMill79
ID: 10795226
Nevermind...I see it...it's 6/(5x1)
0
 

Author Comment

by:GJMill79
ID: 10795227
Thank you!
0
 

Author Comment

by:GJMill79
ID: 10795241
Actually I didn't calculate that right...can you explain that again?  how does work?
5 * 1 % 6 = 5
5 * 2 % 6 = 4
5 * 3 % 6 = 3
5 * 4 % 6 = 2
5 * 5 % 6 = 1
5 * 6 % 6 = 0
5 * 7 % 6 = 5
5 * 8 % 6 = 4
5 * 9 % 6 = 3
5 * 10 % 6 = 2
5 * 11 % 6 = 1
0
 
LVL 3

Expert Comment

by:idt
ID: 10795476
In the above posted results:

5 * 1 % 6 = 5  // in this case there was not a Proper Divisor, since 6 divided into 5, 0 times with 5 remainder
5 * 2 % 6 = 4
5 * 3 % 6 = 3
5 * 4 % 6 = 2
5 * 5 % 6 = 1  // in this case there was not a Proper Divisor, since 6 divided into 25, 4 times with 1 remainder
5 * 6 % 6 = 0  // in this case there was a Proper Divisor, since 6 divided into 30 exactly 5 times with no remainder (0).
5 * 7 % 6 = 5  // in this case there was not a Proper Divisor, since 6 divided into 35, 5 times with 5 remainder
5 * 8 % 6 = 4
5 * 9 % 6 = 3
5 * 10 % 6 = 2
5 * 11 % 6 = 1

(5 * 11) = 55
55 / 6 = 9.1666666666667
but since these are integers and not floats the quotiant would be 9.
(5 * 11) - ( 9 * 6) = 1 remaining, the result of the Modulus  operator.

The code that you have ensures that you can never attempt to reference an array element that is not 0,1,2,3,4 or 5.

Daniel
0
 

Author Comment

by:GJMill79
ID: 10795533
Thanks again!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question