0

I have a c++ code here. This code is to calculate data inside some files.

#include<stdio.h> #include<iostream> #include <fstream> using namespace std; int main(){ //initiate file variable ifstream inFile; //other variable int count, limit=30, time, remain, flag=0, time_quantum, arrival_time=30, var, total_process[30], data; int wait_time=0, turnaround_time=0, rt[30], num_of_interrupts=0, num_of_jobs=0; cout<<"Your job list :\n"; for(var = 1; var <= limit; var++){ //the purpose is the check weither looping is okay or not cout<<""<<var; //preparing array for the file char nambuf[30]; std::snprintf(nambuf, sizeof(nambuf), "job%d.txt", var); std::ifstream inFile; //open file inFile.open(nambuf); //check file if (!inFile) { cout << " Unable to open file"; exit(1); // terminate with error } //read data from file .txt while (inFile >> data) { //calculate total process from data total_process[var] += data; ++data; } //close file inFile.close(); //print total process cout << " Sum = " << total_process[var] << endl; } return 0; 

}

The code was run as it supposed. But the problem occur after performing total process calculation. Example of output :

It give some improper value

Sorry if the code was not good in design. I'm still new in programming.

4
  • Welcome to Stackoverflow. Please note that C and C++ are different languages. Do not include the tag for both (unless the question is somehow really about both). Commented Dec 1, 2019 at 10:46
  • Noted. I will working more on that forward. Thank you. Commented Dec 1, 2019 at 10:54
  • Also, for future questions you should try to avoid posting images of text. It is better to just paste the text as text into a question. Commented Dec 1, 2019 at 10:55
  • I'm not the greatest with C++ but isn't it considered a bit faux pas to mix use of the stdio library with the C++ iostream library? Commented Dec 1, 2019 at 10:57

2 Answers 2

2

There are some issues.

1) Arrays in C++ are indexed from 0, not from 1. That means that when you have an array of 30 elements, allowed indices are from 0 to 29. But in your loop var iterates from 1 to 30, so the last iteration tries to use total_process[30] when the last 'truly' accessible element is total_process[29]. Such errors can be very difficult to debug, as when you write an element out of bounds of your array, you corrupt surrounding memory, so you can change some other variable in that way. To fix this, either iterate with for (var = 0; var < limit; var++), or use var - 1 index like this: total_process[var - 1].

2) Variables and arrays of primitive types such as int are left uninitialized by default, and you shouldn't access such uninitialized variables. Always make sure that when you use some variable it already has some assigned value. You can initialize your array with zeros like int arr[30] = {0};, int arr[30] = {} or int arr[30]{}.

Also be careful and don't get confused with the first way to initialize an array:

int arr[30] = {1}; 

This doesn't initialize all the elements with 1, but just initializes arr[0] with 1 and all the other elements with 0. It works so because you can do this:

int arr[30] = {1, 2, 3, 4, 5}; // all other will be 0 
Sign up to request clarification or add additional context in comments.

1 Comment

i see. i miss look on that just now. Thanks in advance.
0

I think you never initialized your result array total_process.

So you should zero-initialize it with

 int total_process[30] = { 0 }; 

When a program is not behaving like you think it should, you should be using a debugger. That way you can step through the execution and see where it has values or calculations that you didn't expect.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.