Hello, is this for loop possible? Im trying to get each new value for for(a;a<sum;a++) sum variable. After countless tries i am stuck and don't think it's possible to change that... When sum = sum + laikai[i+1]; exists compiler CMD window is not responding and checking for solution... Help would be appreciated! :)

    for(int i=0;i<zaidejai;i++) {
            for(a;a<sum;a++) {
                    if(zaide[a] > 0) {
                                daug = daug + zaide[a];
                                }
                    a += 1;
                    }

                    if(daug > temp) {
                            norm = i;
                            temp = daug;
                            }
            //line below makes compiler stop if i disable this line everything works..
            sum = sum + laikai[i+1];
            }

Recommended Answers

All 8 Replies

Hi, I really don't know if this will help but why not something like:

for(int i=1;i<zaidejai;i++) {
for(a;a<sum;a++) {
if(zaide[a] > 0) {
daug = daug + zaide[a];
}
a += 1;
}
if(daug > temp) {
norm = i;
temp = daug;
}
//line below makes compiler stop if i disable this line everything works..
sum = sum + laikai[i];
}

I could be wrong but having laikai[i+1] may result in going past the bounds of the array. That's my impression but I can't test this so I don't know for sure. Plus, the array laikai has to be the same size as zaidejai for this to work too, doesn't it?

Thanks for respoding. Both arrays are same size and your correction is logical, but i still get the same crash. If i change sum = sum + 5 for example i get no crash but output funny numbers. It's like i cant make sum out of 2 variables.

Did you try

sum += laikai[i];

If you are getting no errors by using 5 instead of laikai than the problem is obviously with the laikai array. Can you show where/how it's declared and where/how it has been populated?

I think you have a problem at lines 2 and 3

//
for(a;a<sum;a++) {
    if(zaide[a] > 0) {

The initial expression at line 2 a looks wrong, did you mean a=0? Otherwise a is just forever getting bigger and you have not shown where you initialise a.

Then at 3 I think you have another array bounds problem. Again you have not supplied enough information to be sure but sum is being incremented in a manor that suggests it is going up quite quickly and if you remove the line incrementing sum your code works. sum going up allows the value of a to go up which could break the bounds of zaide.

Also strangly at line 6 you increment the value of a just before incrementing the value of a in the loop third expression.

commented: Bam, I think you nailed that and I wish I saw it. +5

int laikai[7];

i ran a for loop just to be sure and the output was: 5 5 7 5 1 5 5

U1.txt
8
9 5 7 -5 13 -4 11
7 5 -3 12 -5 17 -3
25 7 12 -3 5 -5 7 -5 3
14 5 12 -3 10 -7 8
5 1 -40
33 5 15 -5 9 -3 8
11 5 -12 8 -5 12 -3
13 5 3 -4 25 -5 3

this is whole code if you have the chance to test it

#include <iostream>
#include <fstream>
#include "conio.h"
#include <cstdlib>

using namespace std;

int skaityti(int zaidejai, int numeriai[], int laikai[], int zaide[]);

int main() {

    int zaidejai;
    int numeriai[7];
    int laikai[7];
    int zaide[50];
    int krepsininkai[8];
    int a = 0;
    int ab = 0;
    int max[8];
    int alpha = 0;

    skaityti(zaidejai,numeriai,laikai,zaide);
    //PIRMI I AIKSTELE ATEJA KREPSININKAI---------------------------------------
    for(int i=0;i<96;i++) {
            for(int t=0;t<8;t++) { // sukuria riba ir suka cikla
                    if(i != max[t]) {  // tikrina kad nepaimtu vel to pacio   
                         if(i==numeriai[ab] && zaide[alpha] >= 0) {
                                            krepsininkai[a] = numeriai[ab];
                                            max[a] = i;
                                            i = 0;
                                            a += 1;
                                            ab += 1;
                                            alpha = alpha + laikai[ab-1];
                                        } else if(i==numeriai[ab] && zaide[alpha] <= 0) {
                                                                ab += 1;
                                                                max[ab] = i;
                                                                alpha = alpha + laikai[ab-1];
                                                                i = 0;
                                                                }
                         }                                      
                    }
            } // for
    // RYKIAVIMAS---------------------------------------------------------------
    int laiko;
    for(int i=0;i<50;i++) {
            for(int a=0;a<4;a++) {
                    laiko = krepsininkai[a];
                    if(krepsininkai[a+1] < krepsininkai[a]) {
                                         krepsininkai[a] = krepsininkai[a+1];
                                         krepsininkai[a+1] = laiko;  
                                         }
                    }
            }
    //TOP 5 IRASYMAS------------------------------------------------------------                
    ofstream irasom_top;
    irasom_top.open("rezultatai.txt");
    for(int i=0;i<5;i++) {
            irasom_top << krepsininkai[i] << " ";
            }
    irasom_top.close();

/*  8
    9 5 7 -5 13 -4 11
    7 5 -3 12 -5 17 -3
    25 7 12 -3 5 -5 7 -5 3
    14 5 12 -3 10 -7 8
    5 1 -40
    33 5 15 -5 9 -3 8
    11 5 -12 8 -5 12 -3
    13 5 3 -4 25 -5 3 */

    //IESKOM KAS ILGIAUSIAI ZAIDE-----------------------------------------------
    int daug = 0;
    int sum = 0;
    int temp = 0;
    int norm = 0;
    int aha = 0;
    a = 0;
    //sum = laikai[0];

    for(int i=0;i<8;i++) {
            cout << laikai[i] << " ";
            }


    for(int i=0;i<zaidejai;i++) {
            //sum += laikai[i];
            for(a;a<sum;a++) {
                    if(zaide[a] > 0) {
                                daug = daug + zaide[a];
                                }
                    }
                    if(daug > temp) {
                            norm = i;
                            temp = daug;
                            }
            }
    //cout << numeriai[norm] << " " << temp;



    _getch();

}

int skaityti(int zaidejai, int numeriai[], int laikai[], int zaide[]) {

    int a = 0;
    int m = 0;

    // Reading
    ifstream skaitom;
    skaitom.open("U1.txt");
    skaitom >> zaidejai;
    for(int i=0;i<zaidejai;i++) {
            skaitom >> numeriai[i];
            skaitom >> laikai[i];
            m = m + laikai[i];
            for(a;a<m;a++) {
                    skaitom >> zaide[a];
                    }
            }     
    skaitom.close();
    }

The initial expression at line 2 a looks wrong, did you mean a=0? Otherwise a is just forever getting bigger and you have not shown where you initialise a.

Then at 3 I think you have another array bounds problem. Again you have not supplied enough information to be sure but sum is being incremented in a manor that suggests it is going up quite quickly and if you remove the line incrementing sum your code works. sum going up allows the value of a to go up which could break the bounds of zaide.

Also strangly at line 6 you increment the value of a just before incrementing the value of a in the loop third expression.

The a variable needs to go up and i really dont brake any limits since the zaide array is quite long and i run an outer loop from 0 to 8

Ok i figured it out thank you all for responding.
The error was in this line i changed zaidejai to 8 and it doesnt crash, however zaidejai is equal to 8, i dont understand what was wrong but im happy its working again.

for(int i=0;i<zaidejai;i++) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.