for (int i = 0; i < 100; i = (i%10))
{
age = (age +10);
int_year = (int_year + 10);
So I am trying to use the Mod function for looping in C#, the requirement is that it the int_year and age loop 100 times but only every 10th increment is kept. Thanks in advance :)
Why not just mod 10 within the loop to determine if this is the 10th iteration:
for (int i = 0; i < 100; i++)
{
if (i % 10 == 0)
{
// Every 10th iteration
}
}
You can use step equal to 10:
for (int i = 0; i < 100; i += 10)
{
// Every 10th iteration
}
I think it's more efficient than using mod.
Related
I'm new at programming in general and learning C# right now.
I just wrote a little programm where I have to step through an int[] in a specific pattern. The pattern is as follows:
Start at the last entry of the array (int i)
form the sum of i and (if avaible) the three entrys above (e.g. i += i-1 ... i += i-3)
Change i to i -= 4 (if avaible)
Repeat from step 2 until i = 0;
Therefore i wrote the following loop:
for (int i = intArray.Length - 1; i >= 0; i -= 4)
{
for (int a = 1; a <= 3; a++)
{
if (i - a >= 0)
{
intArray[i] += intArray[i - a];
intArray[i - a] = 0;
}
}
}
Now my new assignment is to change my code to only use 1 loop with the help of modulo-operations. I do understand what modulo does, but i can't figure out how to use it to get rid of the second loop.
Maybe somebody explain it to me? Thank you very much in advance.
While iterating over the array, the idea would be to use the modulo 4 operation to calculate the next index to which you will add the current value.
This should work with any array lengths:
for (int i = 0; i < intArray.Length; i++){
// we check how far away we are from next index which stores the sum
var offset = (intArray.Length - 1 - i) % 4;
if (offset == 0) continue;
intArray[i+offset] += intArray[i];
intArray[i] = 0;
}
iterating the array reversely makes it a bit more complicated, but what you wanted to get rid of inner loop with modulo is probably this:
var intArray = Enumerable.Range(1, 15).ToArray();
for (int i = 1; i < intArray.Length - 1; i ++)
{
intArray[i - (i % 4)] += intArray[i];
intArray[i] = 0;
}
I am a beginner, trying to solve Project Euler problem 1:
"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.", but, you probably know this problem.
So I get a correct answer but my program lists all sums up to a final sum. But how do I manage to just print final sum?
int x = 0;
for (int i = 1; i < 1000; i ++)
{
if (i % 3 == 0 || i % 5 == 0)
{
Console.WriteLine(x += i);
You have a Console.WriteLine inside a loop, so every time the if is true, a print out to the console will occur. Move the Console.WriteLine so it is outside of the loop
I think you would have quite quickly discovered this problem if you had used the debugger to step through the code line by line. Do you know how to use the debugger? If not, drop a comment and I'll write some introductory lines
You should keep console.WriteLine() outside of 'for' loop,
Your code should go look like this-
int x=0;
for (int i = 1; i < 1000; i ++){
if (i%3==0 || i%5==0){
x+=i;
}}
console.WriteLine(x);
good start bro, you can just make the Console.WriteLine(x) after the for loop
You don't need to write every time you add i to x. You can just add it and then print after you ran the for loop.
int x = 0;
for (int i = 0; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
x += i;
}
}
Console.WriteLine(x);
If you want to print only the final result, move the Console.WriteLine command out of the iteration and place it after it. This way it will execute only once.
int x = 0;
for (int i = 1; i < 1000; i ++)
{
if (i % 3 == 0 || i % 5 == 0)
{
x += i;
}
}
Console.WriteLine(x);
I wrote this method for finding the longest increasing sub sequence in an array. My question is, after checking if the next index is higher in the second for loop, why do I have to even use the else block afterwards? It doesn't output a correct result if I don't. Why wouldn't it just be sufficient to increment currentSeq by 1 if the next one in the sequence is bigger? If false, than just move on with the flow of control, with currentSeq being left at 1 anyway?
static void Main(string[] args)
{
int length = int.Parse(Console.ReadLine());
int[] nums = new int[length];
int currentSeq = 1;
int maxSeq = 1;
for (int i = 0; i < nums.Length; i++)
{
nums[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < nums.Length - 1; i++)
{
if (nums[i] < nums[i + 1])
{
currentSeq++;
}
else
{
currentSeq = 1;
}
if (currentSeq > maxSeq)
{
maxSeq = currentSeq;
}
}
Console.WriteLine(maxSeq);
}
You need the else keyword to reset currentSeq to 1 for the next sequence calculation. Imagine that you're done with the first sequence and you start calculating the next one in the table, you'll need to restart from 1.
I'm trying to store an unknown amount of data into an array, while using a forloop to get data! My task is to find and sum all the numbers form 1 to 1000 that can be divided be 3 and 5.
for (int i = 1; i < 1001; i++)
if (i%3==0)
{
if (i%5==0)
{
//this doesn't work, have tried to convert it to string, didn't work either
int[] array = { i };
//trying to loop the values
for (int j = 0; j < array.Length; i++)
{
//how can I loop this so I dont have to write it all out?
int sum1 = array[j]
}
}
}
Console.ReadKey();
Just because computers can perform repetitive task well doesn't mean you ignore Mathematics. If I got it right, you are trying to find the sum of all the numbers less than 1000 which are divisible by both 3 and 5. So that boils down to all the multiples of 15. Now if you take the floor of 1000/15, you get the the last multiple, which in this case is 66. So, you have to sum the series:
15, 15*2, 15*3,...15*66
=15*(1+2+3+..+66) [15*sum of first 66 positive natural numbers]
=15*66*67/2
So generalizing, finding sum of all numbers less than a and divisible by b is given by:
limit = floor(a/b);
sum = b*limit*(limit+1)/2;
Something like this:
var ListOfInts=new List<int>();
for (int i = 1; i < 1001; i++) {
if (i % 3 == 0 && i % 5 == 0)
ListOfInts.Add(i);
}
var result = ListOfInts.Sum();
Perhaps this code does what you want:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> li = new List<int>();
for (int i = 1; i <= 1000; i++)
{
if (i%3 == 0 && i%5 == 0)
{
li.Add(i);
}
}
Console.Write("sum is " + li.Sum());
Console.ReadLine();
}
}
}
The number devides by 3 and 5 means it devides by 15. So you can start iterating from 15 and incrementing by 15 to skip some iterations:
int sum = 0;
for (int i = 15; i <= 1000; i += 15)
sum += i;
Thanks guys! Alot of good answers, i'm still trying to understand some of them but thanks :)
How come that
List<int> li = new List<int>();
for (int i = 1; i <= 1000; i++)
{
if (i%3 == 0 && i%5 == 0)
{
li.Add(i);
}
}
Console.Write("sum is " + li.Sum());
Console.ReadLine();
give me this
while the code down under
var ListofInts = new List<int>();
for (int i = 0; i < 1001; i++)
{
if (i%3==0 && i%5==0)
{
ListofInts.Add(i);
var result = ListofInts.Sum();
Console.Write(result + ", ");
}
}
gives me this?
I have a code here and I would like that it will display the first 10 and if I click on that, it will display again the second batch. I tried this first with my first for-code and it work now I'm working with arrays it seems it didn't accept it
The one I commented dont work? is this wrong?
Thanks
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i] = potenzen[i-1] * 2;
//if (potenzen % 10 == 0)
// Console.ReadLine();
}
foreach (long elem in potenzen)
{
Console.WriteLine(" " + elem);
}
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i-1]);
if (i % 10 == 0)
Console.ReadLine();
}
is more in line with what you want. An improvement would be to separate your data-manipulation logic from your data display logic.
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
potenzen[i]=potenzen[i-1]*2;
for (int i = 0; i < potenzen.Length; ++i)
{
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
Console.ReadLine();
}
Of course, you could do this without an array
long potenzen = 1;
for (int i = 1; i < 32; ++i)
{
Console.WriteLine(potenzen);
potenzen = potenzen * 2;
if (i % 10 == 0)
Console.ReadLine();
}
You need:
if (i % 10 == 0)
and not:
if (potenzen % 10 == 0)
Applying the modulus operator to an array of longs is dubious.
potenzen is an array so you maybe try
if (i % 10 == 0)
or maybe
if (potenzen[i] % 10 == 0)
You're taking an array mod 10 -- at best, in an unsafe language, you'd be doing the modulo operation on a memory address.
This should work fine if you just change the line to:
// if you don't want to pause the first time you run it, replace with:
// if (i > 0 && i % 10 == 0) {
if (i % 10 == 0) {
Console.ReadLine();
}
Try changing it to:
long [] potenzen = new long[32];
potenzen[0] = 1;
Console.WriteLine(potenzen[0]);
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
{
var s = Console.ReadLine();
// break if s == some escape condition???
}
}
Right now, you're never printing, unless you completely finish your first for loop. My guess is that you're not allowing the full 32 elements to complete, so you're never seeing your results -
This will print them as they go.