|
Hey Guys,
I read this article regarding " Count from 1 to 1000 without using loops "
=============================================================================
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(&main + (&exit - &main)*(j/1000))(j+1);
}
The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system.
=============================================================================
I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all.
Please help me out.
Regards,
Amrit
|
|
|
|
|
The line
(&main + (&exit - &main)*(j/1000))(j+1);
is evaluated each time thus:
&exit - &main is the offset from the start of main to the start of exit
j/1000 will evaluate to zero for all vaules of j less than 1000
Multiplying those two values together gives zero if j is less than 1000.
Add that to &main (the address of main ) and you get the same address, so main gets called with the parameter value j+1 .
This continues until the value of j reaches 1000 at which time:
j/1000 results in the value 1
That is multiplied by the offset of exit which returns that offset.
Add that value to main and the next call will go to exit rather than main .
|
|
|
|
|
Thanks Rechard for this clear explanation.
|
|
|
|
|
Very interesting!
I've never seen this code.
If I wanna plus all numbers between 1 to 1000 with out loops, how can I do it like above method?
Thank you for you advice!
|
|
|
|
|
Use a static variable and just add the value of j to it each time.
|
|
|
|
|
Thank you!
|
|
|
|
|
I have an application developed in non-CLR C++ (unmanaged) in VS2003 (or earlier) but working with VS2005 after some converting. Note that I didn’t do that converting.
My employer decided to update the code to CLR VS2010 for easier maintenance. I am a C# programmer and haven’t done C++ for a while and not within VS so I’m not the most skilled in this task.
I’ve set it to CLR and corrected the errors and most of the warnings, but I’m having a problem with some image resources in the Form1.resX file (took me a while to even figure out that the resources were IN that .resx file I can see them in there and the Form [Design] editor can see the resource and paints the image as a background for its tab:
this->tabPage1->BackgroundImage = (safe_cast<system::drawing::image ^="">(resources->GetObject("tabPage1.BackgroundImage")));
The resX file is version 1.3 of the schema or so it says rather than the 2.0 that a newly created windows forms application has but seems OK otherwise.
<root>
modified 21-May-14 16:11pm.
|
|
|
|
|
|
Well it's not Managed C++/CLI that is for sure.
The makefile mentions gcc and you can find instructions on how to use gcc and a makefile here[^]
|
|
|
|
|
It looks very much like UNIX: all those shell scripts are a dead giveaway. So load it into your UNIX/Linux system, type 'make' and away you go.
|
|
|
|
|
(just realized this question is really old! oh well - might help someone)
I recently ran into a similar issue.
This worked for me:
- In Visual Studio create a C++ Win32 console application
- Add the .C files to the solution (add...existing item)
- Compile - default output is .exe
(The makefile seems to just compile the code - I think the .sh scripts might cleanup after the compiler. But, yeah, the makefile is designed to run in a linux/unix)
Erik Westermann
|
|
|
|
|
how to conver any base like binary to any base like hexadecimal and octal or others bases give me code
|
|
|
|
|
|
code to convert any base like binary to any base
|
|
|
|
|
Is this a question or a statement?
|
|
|
|
|
Hi Iam trying to convert my Digit To Words class from c# to VC++... And the following line I am facing trouble
String^ strNum;
String^ strNumDec;
String^ StrWord;
strNum = Convert::ToString(Num);
blah..blah...blah...
StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum))) + ((double.Parse(strNumDec) > 0) ? (" and Paise" + cWord3((decimal)(double.Parse(strNumDec)))) : "");
Thanks for the helps
|
|
|
|
|
Paramu1973 wrote: trouble
And that trouble would be what exactly?
Paramu1973 wrote: blah..blah...blah...
is not valid C++ or C# code.
|
|
|
|
|
What type is Num and why are you converting it to a string just so that you can parse it into a double? Please provide more details of exactly what problem you are trying to solve.
|
|
|
|
|
Thanks Richard...Still I remember your powerful helps.....Num is Decimal and I found the solution...
Thanks Again
|
|
|
|
|
Is it really C# code?
|
|
|
|
|
Create an STL vector of 10 integers and store initial values (0 to 9 is fine).
Use the std::random_shuffle algorithm to shuffle the list.
Print the list. (try to do this one with an std iterator)
Use the std::sort algorithm to sort the list.
Print the list again. (you can use for each instead of an iterator)
See if you can locate the documentation for random_shuffle and sort either in VS help or with Google. Once you leave this class, you will need to be able to find information like this on your own.
Checklist
1. Did NOT create a folder for the solution
2. Project/solution named correctly
3. Correct comments at top
4. Consistent indentation
5. Good variable names
6. Overall neat organization
7. Comments in code explain what's being done
8. Correct division of code into .h and .cpp files
9. Use of #pragma once in .h files (or, #ifndef)
10. #include "stdafx.h" in cpp files (or, suppress pch)
|
|
|
|
|
|
What does this mean? If you expect me to do your work for you then you are going to be disappointed.
|
|
|
|
|
hi this code is a part of keecak function.
i do not undrastand what is "C[index(x-xOff)];" plz help me;
template<class Lane>
void KeccakF::inverseTheta(vector<Lane>& A) const
{
vector<Lane> C(5);
for(unsigned int x=0; x<5; x++) {
C[x] = A[index(x,0)];
for(unsigned int y=1; y<5; y++){
C[x] ^= A[index(x,y)];
}
}
const LaneValue inversePositions64[5] = {
0xDE26BC4D789AF134ULL,
0x09AF135E26BC4D78ULL,
0xEBC4D789AF135E26ULL,
0x7135E26BC4D789AFULL,
0xCD789AF135E26BC4ULL };
vector<LaneValue> inversePositions(5, 0);
for(unsigned int z=0; z<64; z+=laneSize)
for(unsigned int x=0; x<5; x++)
inversePositions[x] ^= inversePositions64[x] >> z;
for(unsigned int z=0; z<laneSize; z++) {
for(unsigned int xOff=0; xOff<5; xOff++)
for(int x=0; x<5; x++)
for(unsigned int y=0; y<5; y++)
if ((inversePositions[xOff] & 1) != 0)
A[index(x, y)] ^= C[index(x-xOff)];
for(unsigned int xOff=0; xOff<5; xOff++) {
ROL(C[xOff], 1);
inversePositions[xOff] >>= 1;
}
}
}
|
|
|
|
|
index() is a function that takes two values and returns another value. You need to find the documentation for it to know what all the values represent.
|
|
|
|