0

I am a beginner to coding and trying to make simple data visualizations using processing. In order to test, i created a csv file where three columns of data are listed where the 2nd and third are numbers. I parsed the columns and tried dividing the 2nd column of numbers with the 3rd. But unfortunately the results are showing zero.

the code is

String tag = "dataset.csv"; String [] rawData;

int [] marks = new int[6]; int [] maxMarks = new int[6];

float [] percentage = new float[6];

void setup() {   size(800, 800);   smooth();

  rawData = loadStrings(tag);

  for (int i = 1; i<rawData.length; i++) {     String [] thisRow = split(rawData[i], ",");

    String subject = thisRow[0]; //// name of the subject     marks[i-1] = int(thisRow[1]); //// marks recieved     maxMarks [i-1] = int(thisRow[2]); //// maximum marks     percentage [i-1]    = (marks[i-1]/maxMarks[i-1]);   }   println(marks); //// prints the numbers 80,45,40,25,30,40   println(maxMarks); //// prints the numbers 100,50,50,50,50,50   println(percentage); //// prints 0.0,0.0,0.0,0.0,0.0,0.0, }

void draw() { }

I wanted the percentage to be calculated and displayed. :(

It would be a great help if someone can help me to sort this out. Many thanks in advance!

Cheers, Yousuf

1
  • you are getting 0 as result. there are many situations where this can occur.what is the data type of marks[i-1] and maxMarks[i-1]. is it going out of data type range? check out the possible cases. Commented Oct 12, 2015 at 10:01

1 Answer 1

1

Are you using c++ ?

haha , you need to know , an integer divide integer , the result must be an integer !

you want to get a percent , that is a float value .

so just cast it !

static_cast<float>(int_value ) [ C++ style ]

or

float(int_value) [c style] is both ok ~

so , just change here :

percentage [i-1]    = (marks[i-1]/maxMarks[i-1]);

to

percentage [i-1]    = (float(marks[i-1])/maxMarks[i-1]) * 100 ;

or

percentage [i-1]    = (static_cast<float>(marks[i-1])/maxMarks[i-1]) * 100;

*100 , because you want a percent value

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much 小文件! Such a simple one :) I am using Processing which works based on Java. I just turned the integers into float, and now it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.