|
Is it a generational thing? It seems more and more that folks are jumping right into chapter 7 of the book and then getting completely lost when things from the previous 6 chapters are being referenced and they can't understand why or where they came from.
"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
modified 28-Dec-17 12:14pm.
|
|
|
|
|
It seems to be, and we are seeing more and more of this type of question here. I guess they have all bought into the idea that you can make a lot of money as a developer with minimal training. Just pray they are not working at your bank or local hospital.
|
|
|
|
|
here is the code i have wrriten but when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1. What is wrong in the code.
#include <stdio.h>
#include <string.h>
int main()
{
int i, log, k, c1=0, c2=0, j=0;
char team_1[100], team_2[100], input[100], ch;
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
if(strcmp(team_1, team_2)==0)
{
printf("Oops! both names are same. Please enter again: \n");
while(strcmp(team_1, team_2)==0)
{
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
}
}
printf("Enter the number of logs: \n\n");
scanf("%d", &log);
for(i=0; i<log; i++)
{
printf("Enter log-%d\n", i+1);
gets(input);
if(strcmp(input, team_1) == 0)
c1++;
else if(strcmp(input, team_2) == 0)
c2++;
for(k=0; k<=strlen(input); k++)
{
input[k]=0;
}
}
if(c1>c2)
printf("Team-1 wins");
else
printf("Team-2 wins");
return 0;
}
|
|
|
|
|
Tarun Jha wrote: when reaching the for()loop it just skips though the first loop of for( i.e. i=0 )and starts with i=1
How did you find out it? Did you debug your code? Then what are the values of i and log right before stepping in the loop?
|
|
|
|
|
This is the output
Enter name of Team-1:
rob
Enter the name of Team-2
max
Enter the number of logs:
5
Enter log-1
Invalid entry !
Enter log-2
max
Enter log-3
max
Enter log-4
rob
Enter log-5
max
Team-2 wins
Process returned 0 (0x0) execution time : 23.573 s
Press any key to continue.
|
|
|
|
|
Tarun Jha wrote: Enter the number of logs:
5
Enter log-1
And there is your code:
for(i=0; i<log; i++)
{
printf("Enter log-%d\n", i+1);
So why do you think the loop skips when i is zero??? It works and prints the result
"Enter log-1"
|
|
|
|
|
no, it doen't take input for first loop, it just skips to the second one, does'nt matter from where i is initialized.
Please run the code you will understand.
|
|
|
|
|
Why are you asking for both team names if they are equal? You already have team_1 name so you only need a different name for team_2 . You do not need to clear the input array as it will get overwritten on the next gets call. And it skips the first loop because scanf leaves a newline character in the input buffer which then is consumed by the first call to gets . If you did as I have suggested elsewhere you could figure all these things out for yourself.
|
|
|
|
|
are you suggesting that i initialize input to zero before applying gets() as you told me to do in the previous question answered by you.
|
|
|
|
|
No, I am telling you to go and get a proper study guide and learn the C language in complete detail.
|
|
|
|
|
i will but befoer that can you show me how to correct the buffering problem of scanf() in order to remove newline charecter from gets().
|
|
|
|
|
|
And here is the working code
#include <stdio.h>
#include <string.h>
int main()
{
int i, log, j, c1=0, c2=0;
char team_1[100], team_2[100], input[100], ch;
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
if(strcmp(team_1, team_2)==0)
{
printf("Oops! both names are same. Please enter again: \n");
while(strcmp(team_1, team_2)==0)
{
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
}
}
printf("Enter the number of logs: \n");
scanf("%d%*c", &log);
for(i=0; i<log; i++)
{
printf("\nEnter log-%d\n", i+1);
gets(input);
if(strcmp(input, team_1) == 0)
c1++;
else if(strcmp(input, team_2) == 0)
c2++;
else
printf("Invalid entry !\n");
for(j=0; j<=strlen(input); j++)
{
input[j]=0;
}
}
if(c1>c2)
printf("\nTeam-1 wins");
else if(c1<c2)
printf("\nTeam-2 wins");
else
printf("\nIt's a tie");
return 0;
}
|
|
|
|
|
And here is the working code..
#include <stdio.h>
#include <string.h>
int main()
{
int i, log, j, c1=0, c2=0;
char team_1[100], team_2[100], input[100], ch;
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
if(strcmp(team_1, team_2)==0)
{
printf("Oops! both names are same. Please enter again: \n");
while(strcmp(team_1, team_2)==0)
{
printf("Enter name of Team-1: \n");
gets(team_1);
printf("Enter the name of Team-2\n");
gets(team_2);
}
}
printf("Enter the number of logs: \n");
scanf("%d%*c", &log);
for(i=0; i<log; i++)
{
printf("\nEnter log-%d\n", i+1);
gets(input);
if(strcmp(input, team_1) == 0)
c1++;
else if(strcmp(input, team_2) == 0)
c2++;
else
printf("Invalid entry !\n");
for(j=0; j<=strlen(input); j++)
{
input[j]=0;
}
}
if(c1>c2)
printf("\nTeam-1 wins");
else if(c1<c2)
printf("\nTeam-2 wins");
else
printf("\nIt's a tie");
return 0;
}
|
|
|
|
|
You have identified "problem area " , so work on problem area first.
No matter how many books you read , they will NEVER tell you that
MAJORITY of errors are typos, and than you will have hidden SYNTAX errors.
The last errors are programming - LOGICAL errors.
Before you compare real INPUTS, you have to have correct program sequence working so emulating the actual inputs would be next step.
You are starting Backward with analysis of logical / real input errors.
PS You have a "numerical" application , so prefacing printed numbers with "minus" sign" seems little odd.
for(i=0; i<log; i++)
{
#ifdef DEBUG
printf( "Variable i = %d", i);
#endif
printf("Enter log-%d\n", i+1);
#ifdef DEBUG
printf( "Variable i = %d", i);
#endif
gets(input);
if(strcmp(input, team_1) == 0)
c1++;
else if(strcmp(input, team_2) == 0)
c2++;
for(k=0; k<=strlen(input); k++)
{
input[k]=0;
}
}
|
|
|
|
|
Dear Vaclav_,
could you edit your post to properly format the code snippet?
Otherwise it is very hard to read/understand.
|
|
|
|
|
it works too..
<pre>#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int log, i, j, k, count1=1, count2=0, c;
printf("Enter the number of logs: \n");
scanf("%d%*c", &log);
char names[log][100];
printf("Enter names: \n");
gets(names[0]);
for(i=1; i<log; i++)
{
gets(names[i]);
if(strcmp(names[i], names[0])!= 0)
{
count2++;
k = i;
for(j=k+1; j<log; j++)
{
gets(names[j]);
while((strcmp(names[j], names[0])!=0 && strcmp(names[j], names[k])!=0) || ((c=names[j][0])==EOF && c=='\n')) {
printf("Invalid entry!\n");
memset(names[j], 0, sizeof(names[j]));
gets(names[j]);
}
if((strcmp(names[j], names[0])==0))
{
count1++;
}
else if((strcmp(names[j], names[k])==0))
{
count2++;
}
}
if(count1>count2)
{
puts(names[0]);
printf("Wins !");
}
else if(count2>count1)
{
puts(names[k]);
printf("Wins !");
}
else if(count1==count2)
{
printf("It's a tie!!");
}
break;
}
count1++;
}
return 0;
}
|
|
|
|
|
I am trying to come up with “calling scheme” to be able to go "from top to bottom".
For example “clear LCD device” via I2C interface and connected to GPIO.
The “clear sequence from “top” is to
send device specific command - “clear”
output binary values of “clear command ” to SDA/SCL interface
output binary values to GPIO
I have a 3 levels basic / working inheritance scheme - with class representing the device , class representing I2C communication format and class representing the GPIO.
I am not sure plain direct inheritance of chain of classes is going to do the job.
I think I need to come up with “nesting” scheme instead of just plain direct inheritance chain using only instance of the “top” class.
Any suggestions will be appreciated.
Something like this, but it looks too scary
http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_254.html
Cheers
Vaclav
PS Please do not ask for code or "what is it for ? " , this project is under construction and not ready for public scrutiny.
|
|
|
|
|
I would advise you to try some experiments. It can be very simple. Write a simple class hierarchy with derived classes and have them call a virtual method each of them implements. Within the method, have it output a trace statement (TRACE, printf, cout<<, what ever...) You will see the calling sequence by the order of the output. One thing to be careful of is the order of the calls :
virtual void TestMethod( int n )
{
__super::TestMethod( n+1 );
trace( _T( "in TestMethod( %d )\n" ), n );
}
virtual void TestMethod( int n )
{
trace( _T( "in TestMethod( %d )\n" ), n );
__super::TestMethod( n+1 );
}
The output will show you which comes first. Implement as many derivation levels as you want, at least three I think. The base class won't have a __super so you will have to comment that off for it. Anyway, I recommend that you try this experiment. It doesn't take long and it is very informative.
|
|
|
|
|
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction.
"main.cpp"
#include "ESSolver.h"
using namespace std;
int maxIt;
int minVal;
int maxVal;
int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;
if( function_eval == 1){
minVal = -32; maxVal = 32;
}
ESSolver es(nVar, mu, lbd, rhoO, rhoS);
es.function_eval = function_eval;
es.Init();
es.Optimize();
return 0;
}
"ESSolver.cpp"
#include <iostream>
#include <vector>
#include "ESSolver.h"
#include <random>
using namespace std;
ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);
for (int i = 0; i < mu; i++) {
muPop[i].resize(nVar);
muSigma2[i].resize(nVar);
}
lambdaPop.resize(lbd);
lambdaSigma.resize(lbd);
lambdaSigma2.resize(lbd);
for (int i = 0; i < lbd; i++) {
lambdaPop[i].resize(nVar);
lambdaSigma2[i].resize(nVar);
}
mulambdaPop.resize(mu+lbd);
mulambdaSigma.resize(mu+lbd);
mulambdaSigma2.resize(mu+lbd);
for (int i = 0; i < mu+lbd; i++) {
mulambdaPop[i].resize(nVar);
mulambdaSigma2[i].resize(nVar);
}
rhoPop.resize(rhoO);
rhoSigma.resize(rhoS);
rhoSigma2.resize(rhoS);
for (int i = 0; i < rhoO; i++) {
rhoPop[i].resize(nVar);
}
for (int i = 0; i < rhoS; i++) {
rhoSigma2[i].resize(nVar);
}
return;
}
ESSolver::~ESSolver(void) {
return;
}
void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}
if (mutOp == 0) {
for (int i = 0; i < mu; i++) {
muSigma[i] = rand01_r();
}
}
else {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muSigma2[i][j] = rand01_r();
}
}
}
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
it = it + 1;
}
}
int ESSolver::rand_r(int minVal, int maxVal) {
default_random_engine rng( random_device{}() );
uniform_int_distribution<int> dist( minVal, maxVal);
return dist(rng);
}
double ESSolver::rand01_r() {
default_random_engine rng( random_device{}() );
uniform_real_distribution<double> dist(0.0, 1.0);
return dist(rng);
}
"ESSolver.h"
#include <vector>
using namespace std;
extern int maxIt;
extern int minVal;
extern int maxVal;
class ESSolver
{
public:
ESSolver(int, int, int, int, int);
~ESSolver(void);
void Init();
void Optimize();
int rand_r(int, int);
double rand01_r();
private:
int nVar;
int mu;
int lbd;
int rhoO;
int rhoS;
vector<vector<double>> muPop;
vector<double> muSigma;
vector<vector<double>> muSigma2;
vector<vector<double>> lambdaPop;
vector<double> lambdaSigma;
vector<vector<double>> lambdaSigma2;
vector<vector<double>> mulambdaPop;
vector<double> mulambdaSigma;
vector<vector<double>> mulambdaSigma2;
vector<vector<double>> rhoPop;
vector<double> rhoSigma;
vector<vector<double>> rhoSigma2;
};
modified 29-Jan-21 21:01pm.
|
|
|
|
|
seems mostly with vector resize which you are using extensively.
|
|
|
|
|
But they will be executed only once during constructor call
modified 29-Jan-21 21:01pm.
|
|
|
|
|
single run takes more than 1 MB
modified 19-Dec-17 7:01am.
|
|
|
|
|
Actual code has threading. When I ran 10 threads independently, my entire ram of 4GB got full.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
What do you mean by lot of memory?
I think that about 2M are used by the ESSolver instance of your example code.
|
|
|
|
|