For loop runs once and ends program prematurely.

Hey all, this is my first post on this forum so if i'm writing this question wrong or something simple please let me know! Basically i am writing a code for an assignment that is supposed to run through a loop testing different arrays and their maximum amount of people that can be held in their train. For some reason the for loop i have included only runs once, and then quits the program. This is a problem as the for loop should be running 'imax' number of times, and the output for my program is located after the for loop, which is what i will be graded on so the fact that my for loop is quitting the program early is a problem. I have looked over my code for a while and cannot seem to find what is wrong with it that prevents the loop from exceeding one count and stops the program in it's tracks. I'm a newbie coder so sorry for my coding format if it's pretty bad! Thank you all!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 int main(void) {
	#define first_car 10
	#define normal_car 8
	#define car_cap 4

	int trackL, maxTL, maxp, i, key, imax, TL[i], ncars[i], ntrains[i], tcars[i], CCL[i], peop[i];
	float avgR[i];
	
	printf("What is the total length of the track, in feet?\n");
	scanf("%d", &trackL);
	printf("What is the maximum length of a train, in feet?\n");
	scanf("%d", &maxTL);
	
	maxp=0;
	TL[0]=10;
	ncars[0]=1;
	imax=(maxTL-10)/8;

	for(i=0;i<imax;i++) {
		ntrains[i]= maxTL/TL[i];
		tcars[i]=ntrains[i]*ncars[i];
		CCL[i]=ntrains[i]*TL[i];
		peop[i]=tcars[i]*car_cap;
		avgR[i]=peop[i]/CCL[i];
		if(peop[i]>maxp){
			maxp= peop[i];
			key= i;
		}
	}
		
	printf("Your ride can have at most %d people on it at one time\n", maxp);
	printf("This can achieved with trains of %d cars\n", ncars[key]);
	printf("AVG Ratio: %7.3f\n", avgR[key]);
	
	return 0;
	
}
Try debugging your program there are errors.If you are using printf and scanf you must include<stdio.h> it is for C programs.For C++ use std::cin and std::cout.These are in <iostream> library.And you can not put TL[i],ncars[i] and so on.And your imax depend in what the user inputs .
Last edited on
I have include<stdio.h> at the top, just forgot to copy it. I have not learned what std::cin and std::cout are in my programming class yet, so my professor would most likely accuse me of ot writing the code myself if i included those (even if i knew how to include those). The problem still remains after changing the declared arrays, printf functions after the for loop do not execute.
Last edited on
You can create dynamic array but if it is not important for your project you can just put some bigger number like 100.TL[100]-there are 100 places and you can not change it later,but if you have done dynamic allocation you can save space and allocate memory later in the program.But for school purposes you can just allocate static array.
EDIT:
So there are some mistakes.You put TL[0]=10,but in iteration it get bigger and you have not declared
TL[1],TL[2] and so on,also with ncars you have the same problem.Also I am uncertain for maxTl.What is expected from user to insert?If he insert 3 it's a problem.
At beginning all arrays are empty so...You can write whole your task because i do not understand here.
Last edited on
imax=(maxTL-10)/8;

maxTL is read from the user. what did you type in?
if you put in 10, you get 1, integer division 10/8 = 1.
does it work if you put in 80 for maxTL? Should loop 8 times.
Topic archived. No new replies allowed.