3 Replies - 224 Views - Last Post: 12 February 2014 - 07:15 AM Rate Topic: -----

#1 LShall24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-February 14

Error with for loop (entropy program)

Posted 12 February 2014 - 03:32 AM

Fairly new to the game of programming, and I always seem to have problems with the smallest of syntax errors.

The program is supposed to show how entropy tends to increase

#include <iostream>
#include <math.h>
#include <cstdlib>

using namespace std;

int main()
{
const int n = 10;
int A[n], iterations = 0, bound = 100;

int k;
double sum = 0;

for (k=rand()%n;
{


    A[k] = 1 - A[k];
}
for (int j = 0:n-1); {
    sum = sum + A[k];
    cout<< n << sum << sum/n << endl;
    if (iterations < bound);
    goto Loop;
}
}




Here is my error log:

||=== Build: Debug in Lab 3 (compiler: GNU GCC Compiler) ===|
|16|error: expected primary-expression before '{' token|
|16|error: expected ';' before '{' token|
|16|error: expected primary-expression before '{' token|
|16|error: expected ')' before '{' token|
|21|error: range-based 'for' loops are not allowed in C++98 mode|
|21|warning: unused variable 'j' [-Wunused-variable]|
|25|error: label 'Loop' used but not defined|
||=== Build failed: 6 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|



Any help would be greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Error with for loop (entropy program)

#2 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 227
  • View blog
  • Posts: 812
  • Joined: 18-April 09

Re: Error with for loop (entropy program)

Posted 12 February 2014 - 03:48 AM

Hi,

You have several problems in your code.

The first one is the for-loop. The for-loop is something like this:

for(variable initialization; condition; variable update)
{
  // code to loop.
}


you for loop in line 15 does not comply to the above

for (k=rand()%n;


I believe you want to use the for loop for you array
int A[n]
. Here is an example of using an for-loop to print out arrays:

int main()
{
  const int n = 10;
  int A[n] = {1,3,5,2,5,3};

  for (int k = 0; k < 6; k++)
   {
       cout << A[k] << endl;
   }
  return 0;
}


You might also want to check the for-loop on line 21 as well. the semicolon ( ; ) right after your for-loop
for (int j = 0:n-1);


makes the for-loop not evaluated. The variable initialization, conditions and variable updates are also wrong there.

This post has been edited by tarmizi_adam2005: 12 February 2014 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,351
  • Joined: 25-December 09

Re: Error with for loop (entropy program)

Posted 12 February 2014 - 07:02 AM

View Posttarmizi_adam2005, on 12 February 2014 - 04:48 AM, said:

You might also want to check the for-loop on line 21 as well. the semicolon ( ; ) right after your for-loop
for (int j = 0:n-1);


makes the for-loop not evaluated. The variable initialization, conditions and variable updates are also wrong there.


Actually if the for loop were correct it would only seem like the loop was only executed once. The loop "body" would only happen once but the statement would still execute. For example the following snippet will print 100, because the for statement executed 100 times, but it will only print the value once, not 100 different printouts as you may expect by looking at the indentation.

#include <iostream>

using namespace std;

int main(void)
{
   int i = 0;
   for(i = 0; i < 100; ++i);
   {
      cout << i << endl;
   }
   return(0);
}



Jim
Was This Post Helpful? 0
  • +
  • -

#4 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 227
  • View blog
  • Posts: 812
  • Joined: 18-April 09

Re: Error with for loop (entropy program)

Posted 12 February 2014 - 07:15 AM

Ah, I see... can't believe I missed that all this time. Thanks jim for pointing that out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1