Solved

Array

Posted on 2004-04-09
8
238 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
  • 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
 

Author Comment

by:GJMill79
ID: 10795226
Nevermind...I see it...it's 6/(5x1)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now