|
So what's the problem? Can we assume you've stepped through the code, line by line, using the debugger (to see the value of each variable along the way)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
You need to stop writing code for a while and spend more time thinking about the problem. What steps are needed to accomplish your goal? Write them down on paper, line by line to understand what needs to be done at each step. I already showed you how to find the largest number in a sequence and you are still making it more complicated. As you read in the integers in the first loop you can check for the largest as you store them, you do not need a second loop. You also need to look at the statement in the second loop:
largest = num[j];
j=k;
What is the value of k at this point?
And in the final loop you are just moving numbers, with no regard to whether they are in any order.
|
|
|
|
|
What you basically need to do is to sort the array in descending order.
There are several sorting algorithms.
This the first google search link I found for sorting - Sorting Arrays[^]
Go through them, try them and understand them.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
#include <stdio.h>
#define N 50
int main()
{
int num[N], i=0, count =0;
printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do
{
if( scanf("%d", &num[i]) != 1) break;
++i;
count++;
} while ( i<N && num[i-1] !=0 );
int k = 0;
while(k < count-1)
{
int largest_index = k;
for(i=k+1; i<count; ++i)
{
if( num[largest_index] < num[i] )
largest_index = i;
}
int tmp = num[k];
num[k] = num[largest_index];
num[largest_index] = tmp;
++k;
}
for (i=0; i<count; ++i)
printf("%d\n", num[i]);
return 0;
}
As an alternative, tou might use qsort :
#include <stdio.h>
#include <stdlib.h>
#define N 50
int comp(const void * a, const void *b)
{
return *(int *)b - *(int*)a;
}
int main()
{
int num[N], i=0, count =0;
printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do
{
if( scanf("%d", &num[i]) != 1) break;
++i;
count++;
} while ( i<N && num[i-1] !=0 );
qsort(num, count, sizeof(num[0]), comp);
for (i=0; i<count; ++i)
printf("%d\n", num[i]);
return 0;
}
modified 16-Oct-17 9:37am.
|
|
|
|
|
i need to find the biggest number, but it doesn't print the biggest number.
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i=0, j, count =0;
printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do{
scanf("%d", &num[i]);
i++;
count++;
}while(num[i-1]!=0);
for(i=0; i<=count -1; i++){
for(j=0; j<=count-1; j++){
if(i != j)
num[i] > num[j];
else
break;
}
printf("%d is the biggest number", num[i]);
}
return 0;
}
modified 13-Oct-17 12:07pm.
|
|
|
|
|
Tarun Jha wrote: num[i] > num[j];
I suspect that you think that line is doing something that it is not.
Put some curly braces around that line then print out the values before and after it.
|
|
|
|
|
do you mean inside he inner for loop, to print value of i ?
|
|
|
|
|
Print i,j and the array values of each before and after that line.
|
|
|
|
|
Most of that code is redundant, all you need is something like:
int num = -1, largest = 0;
do{
printf("Enter an integer, 0 to exit:\n");
scanf("%d", &num);
if (num > largest)
largest = num;
}while(num != 0);
printf("%d is the biggest number", largest);
|
|
|
|
|
I know that method but i need to know what is wrong with the one i am working with.
|
|
|
|
|
Start with the hint that jschell gave, look at that line of code and see what it is doing if anything, or better yet step though each line of code with a debugger.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Tarun Jha wrote: what is wrong with the one i am working with. Your second loop makes no sense as you are using two index varaibles when you only need one, and you never compare the values to see which is largest, or keep a note of the largest value you have found so far.
|
|
|
|
|
ohh! Thank you.
but the code written below only works some time..
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i=0, j, count =0;
int k, b;
printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");
do{
scanf("%d", &num[i]);
i++;
count++;
}while(num[i-1]!=0);
for(k=0; k<=count-2; k++){
for(j=0; j<=count-2; j++){
if(k != j){
num[k] > num[j];
b = k;
continue;
}
else
break;
}
}
printf("%d is the biggest number", num[b]);
return 0;
}
modified 13-Oct-17 13:39pm.
|
|
|
|
|
Tarun Jha wrote: ...only works some time Which technically means it does not work. If your algorithm is dependent upon its input in order to produce the correct output, it is wrong, period.
You should be able to do this entire exercise using pencil and paper before ever committing anything to code. Short of that and you are just guessing (hoping) at best.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Yes, because your code is still wrong, mainly because there is no logic to it. I gave you the answer in a previous comment, which should at least give you an idea of the logic involved in such a simple task.
|
|
|
|
|
The problem is obvious
if(i != j)
num[i] > num[j];
else
break;
Start with i=0 j = 0 what does the break do ... i==j doesn't it so it will call the break .. single step debug and watch
In vino veritas
|
|
|
|
|
this program does not halt
why ?
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
cin >> in ;
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
}
|
|
|
|
|
bluatigro wrote: why ? Have you stepped through it using the debugger (to see what value in has when you think it should be 'empty')?
The string class may not have a != operator so what you may be comparing is the object itself against a string literal (rather than the contents of the object).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
std::string have != operator. The problem is with cin, it does not takes new line char or leading space chars by default.
|
|
|
|
|
To make it working, change as following:
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
getline(cin, in );
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
}
|
|
|
|
|
Please help: I cannot find the header file for GetTickCount64()
I am using GetTickCount() but that is only good for about 49 days and my program runs forever..maybe.
Anyway, I am using C++ with MFC
I even tried this:
#if (_WIN32_WINNT >= 0x0600)
WINBASEAPI ULONGLONG WINAPI GetTickCount64(void);
#endif
But that only declares it so the compiler doesn't complain and don't need the header file but its not in any library or DLL that I can find.
I have tried searching everywhere on the computer and the internet, what am I doing wrong?
I am using both MSVC2005 and MSVC2015 with very bad luck.
Compilers parameters:
Application (.exe)
Use MFC in a Static Library
Use of ATL: Static Link to ATL
Use Multi-Byte Character Set
No Common Language Runtime support
and
#define WINVER 0x0601
Thanks in advance
Craig
modified 11-Oct-17 17:26pm.
|
|
|
|
|
Hi,
You did not give enough information... what compiler are you using?
What is the target platform?
It sounds like you are using an old compiler. If you are on an older compiler then you may need to download the "Platform SDK" or "Windows SDK"[^] to obtain more recent LIB and headers.
Side Note:
If these older SDK are critical for your business then I highly recommend downloading and keeping an archived copy before the Windows 7 SKU reaches EOL of the 2020 extended support. Once those operating systems reach EOL the SDK download links may disappear.
Best Wishes,
-David Delaune
|
|
|
|
|
Hi and thank you for the help.
The compiler is Microsoft visual Studio 2005 and 2015, (MSVC).
I run the program on these OS's: Windows 7, 8, and 10.
I search both compilers for any header file that contains GetTickCount64 but its not hear, Yes, I google the internet but nothing suggest where it is defined.
Microsoft defines it in Winbase.h but not in my compilers from Microsoft.
Most importantly, what is the LIB that has this function?
Its also helpful if I can find the header file but really not that important, just want the LIB and maybe a file date of the LIB to make sure I have the right one.
|
|
|
|
|
Well,
I can assure you that both of those compilers were distributed with headers and libs that include the GetTickCount64 function. If you are using VS2015 and cannot compile/link with the GetTickCount64 function then you need to fix your headers and compiler macro/defines.
Search all your files for _WIN32_WINNT and WINVER.
Using the Windows Headers[^]
Best Wishes,
-David Delaune
|
|
|
|
|
The function is defined in Winbase.h did you include it?
In vino veritas
|
|
|
|