Jump to content

How do i make an array thats given 2 values, add them and produce an array with results?

matin94

this is for Java and im using eclipse. i need to give an array 2 marks, course work mark and exam marks, add them and produce the results in an array as the module marks.

i know how to make an array and give it values, but i dont know how to produce the results in an array.

Link to comment
Share on other sites

Link to post
Share on other sites

Sooo.... like this?

[X1, X2, X3] + [Y1, Y2, Y3] = [X1 + Y1, X2 + Y2, X3 + Y3]

 

yeah pretty much

do i need 2 different integers for exams and course work, then add them and produce it in an array list (which i dont know how to)

Link to comment
Share on other sites

Link to post
Share on other sites

Sooo.... like this?

[X1, X2, X3] + [Y1, Y2, Y3] = [X1 + Y1, X2 + Y2, X3 + Y3]

 

then you could use a for i++ statement to make it do this from 1..whatever number you want

Link to comment
Share on other sites

Link to post
Share on other sites

Sooo.... like this?

[X1, X2, X3] + [Y1, Y2, Y3] = [X1 + Y1, X2 + Y2, X3 + Y3]

 

just to clear things up

i have 5 modules, so 5 exam marks. 5 course work marks, add each exam and course work mark for each module. and divide them by 2 i think. each exam mark is out of 100 so is the course work mark. i need to add them and divide the total by 2 and produce the results in an array as a the module course. for example, mathematics. 65 exam mark, 50 coursework mark. add them. 115, devide by 2. 57.5 (rounds up to 58), (ill use a double for it) and the that 58 has to come out as "module mark for mathematics 58

Link to comment
Share on other sites

Link to post
Share on other sites

then you could use a for i++ statement to make it do this from 1..whatever number you want

bit confused, ++ is +1. if "i" is an interger. does it mean i + 1? (im quiet new to this)

Link to comment
Share on other sites

Link to post
Share on other sites

ya it basically sets i to 1 and keeps increasing it by 1 until thre greater number (number after the ..) is reached

 

http://patentimages.storage.googleapis.com/WO1995014349A1/imgf000075_0001.png

okay now, the link you have sent, there are a lot of those methods i havent learnt yet. like "NULL", float, sizeof, and the ** after that, i dont no what they mean. theres only so much i can use because of the specs of my project. if i use things we havent learnt yet. i loose marks. i basically need it to be as simple as possible.

Link to comment
Share on other sites

Link to post
Share on other sites

okay now, the link you have sent, there are a lot of those methods i havent learnt yet. like "NULL", float, sizeof, and the ** after that, i dont no what they mean. theres only so much i can use because of the specs of my project. if i use things we havent learnt yet. i loose marks. i basically need it to be as simple as possible.

the main thing i forgot to highlight was the for i = 0; i < 5; i++

Link to comment
Share on other sites

Link to post
Share on other sites

the main thing i forgot to highlight was the for i = 0; i < 5; i++

yeah i just watched a tutorial aswell. they are suggesting 

so first i make my arrays (lets assume they are called first ary and second ary.

the important code is : irb> add_arrays([1,2,3],[4,5,6],[7,8,9])

=> [12, 15, 18]

do you think this will work fine and easily?

 

def add_arrays(first_ary, *other_arys)

first_ary.zip(*other_arys).map{|column| column.reduce(&:+) }

end

irb> add_arrays([1,2,3],[4,5,6])

=> [5, 7, 9]

irb> add_arrays([1,2,3],[4,5,6],[7,8,9])

=> [12, 15, 18]

Link to comment
Share on other sites

Link to post
Share on other sites

Or should i go

 

double firstArray[] = {1,2,3,4,5}

double secondArray[] = {1,2,3,4,5}

 

for (double sum=0; sum<firstArray.length + secondArray.length; sum++);

 

then i would produce it in an array

System.out.println "whatever comes after it"

 

THE reason its doubles instead of int, because the marks can have decimals

Link to comment
Share on other sites

Link to post
Share on other sites

the main thing i forgot to highlight was the for i = 0; i < 5; i++

 

public class computemarks 
{
public static void main (String [] args) {
 
 
System.out.println("Exam Mark\tModule Mark");
int marksexam [] = {1, 2, 3, 5, 6, 7, 8, 9};
for (int counter=0;counter<marksexam.length;counter++)
{
System.out.println(counter + "\t" + "\t" + marksexam[counter]);
}
}
 
}
this is my code so far, now i need both sides to add eachother, but i havent made a separate column for the module mark
Link to comment
Share on other sites

Link to post
Share on other sites

Or should i go

 

double firstArray[] = {1,2,3,4,5}

double secondArray[] = {1,2,3,4,5}

 

for (double sum=0; sum<firstArray.length + secondArray.length; sum++);

 

then i would produce it in an array

System.out.println "whatever comes after it"

 

THE reason its doubles instead of int, because the marks can have decimals

 

For firstArray and secondArray it makes sense to have them be doubles since they are holding doubles. However sum in the for loop 'should' be an int (it will work as a double but you'll only be storing integers). Also assuming I am understanding the problem correctly you will just want to use sum < firstArray.length (or alternatively secondArray.length, or the larger of the two) but not both. Assuming you have something like thirdArray[sum] = firstArray[sum]+secondArray[sum]; in the for loop, looping firstArray.length + secondArray.length will go out of bounds and try to access array slots that don't exist.

Link to comment
Share on other sites

Link to post
Share on other sites

For firstArray and secondArray it makes sense to have them be doubles since they are holding doubles. However sum in the for loop 'should' be an int (it will work as a double but you'll only be storing integers). Also assuming I am understanding the problem correctly you will just want to use sum < firstArray.length (or alternatively secondArray.length, or the larger of the two) but not both. Assuming you have something like thirdArray[sum] = firstArray[sum]+secondArray[sum]; in the for loop, looping firstArray.length + secondArray.length will go out of bounds and try to access array slots that don't exist.

yes that is exactly my problem. i need the element of each array and add them with the other element of the second array, for example add the first element of array A to the first element of array B, second element of array A with the second element of array B and so on . but i need both array A, and B and a new array (results) to show up after each other if that makes sense. right now this is what i have.

 

 

 

 

public class computemarks 
{
public static void main (String [] args)
{
System.out.println("Exam Mark" + "\t" + "Modulemark");
int marksexam [] = {50, 50, 50, 50, 50};
int courseworkmark [] = {50, 50, 50, 50};
int answers [] = new int [marksexam.length + courseworkmark.length];
for (int i = 0; i < answers.length; i++){
answers = marksexam + courseworkmark;
System.out.println(answers);
}
}
 
}
Link to comment
Share on other sites

Link to post
Share on other sites

For firstArray and secondArray it makes sense to have them be doubles since they are holding doubles. However sum in the for loop 'should' be an int (it will work as a double but you'll only be storing integers). Also assuming I am understanding the problem correctly you will just want to use sum < firstArray.length (or alternatively secondArray.length, or the larger of the two) but not both. Assuming you have something like thirdArray[sum] = firstArray[sum]+secondArray[sum]; in the for loop, looping firstArray.length + secondArray.length will go out of bounds and try to access array slots that don't exist.

 

public class computemarks 
{
public static void main (String [] args)
{
System.out.println("Exam Mark" + "\t" + "Coursework" + "\t" + "Modulemark");
double marksexam [] = {50, 50, 50, 50, 50};
double courseworkmark [] = {50, 50, 50, 50, 50};
double answers [] = new double [marksexam.length];
for (int i = 0; i < answers.length; i++){
answers = ((marksexam + courseworkmark) /2);
System.out.println(answers);
}
}
 
}
this worked and it devides both arrays by 2 and produces the answers
Now my problem is, it doesnt print out the course work marks and the exam marks, just prints out the answers. what should i do?
Link to comment
Share on other sites

Link to post
Share on other sites

 

 

public class computemarks 
{
public static void main (String [] args)
{
System.out.println("Exam Mark" + "\t" + "Coursework" + "\t" + "Modulemark");
int marksexam [] = {50, 50, 50, 50, 50};
int courseworkmark [] = {50, 50, 50, 50, 50};
int answers [] = new int [marksexam.length];
for (int i = 0; i < answers.length; i++){
answers = ((marksexam + courseworkmark) /2);
System.out.println(answers);
}
}
 
}
this worked and it devides both arrays by 2 and produces the answers
Now my problem is, it doesnt print out the course work marks and the exam marks, just prints out the answers. what should i do?

 

Print them out like you do the answers. Might wanna add something before the array when displaying so you know whats what. EX System.out.println("Answer: " + answers) or you could even loop through and print out the marks and coursework arrays before you calculate the answers depending on how you/your teacher wants it.

Link to comment
Share on other sites

Link to post
Share on other sites

Print them out like you do the answers. Might wanna add something before the array when displaying so you know whats what. EX System.out.println("Answer: " + answers) or you could even loop through and print out the marks and coursework arrays before you calculate the answers depending on how you/your teacher wants it.

.how do i loop through it to print out the marks for exam and coursework first before calculating the sum?

that is my current problem, also they arent parallel  lines :(

Link to comment
Share on other sites

Link to post
Share on other sites

.how do i loop through it to print out the marks for exam and coursework first before calculating the sum?

that is my current problem

The same way you loop through them to calculate answers[]

 

//declare your arrays and variables

 

for(int i = 0; i < ???; i++){

//print out marks

//print out coursework

}

 

//insert your for loop that calculates answer

Link to comment
Share on other sites

Link to post
Share on other sites

The same way you loop through them to calculate answers[]

 

//declare your arrays and variables

 

for(int i = 0; i < ???; i++){

//print out marks

//print out coursework

}

 

//insert your for loop that calculates answer

okay thanks i'll give it a try. in my specs it said i need to use a new method to calculate marks. what will be the outcome of that? 2 tables first then a table underneath it with answers or else?

 

EDIT: Never mind, i can run different methods, as you probably know, ill use

public static void main (String [] args)

{

        computeMarks or computeResults

}

Link to comment
Share on other sites

Link to post
Share on other sites

okay thanks i'll give it a try. in my specs it said i need to use a new method to calculate marks. what will be the outcome of that? 2 tables first then a table underneath it with answers or else?

It sounds like your instructor wants you to pass in them  marks and coursework arrays to a method and the method will return an array of answers. So your for loop that calculates answers will be in a separate method and then the return type of that method will be int[]. Let me know if I need to clarify a little bit, just trying to guide you in the right direction rather than giving you the right answer. So I would first figure out how to pass in two arrays into a method as paramaters, then how to return an array from a method.

Link to comment
Share on other sites

Link to post
Share on other sites

It sounds like your instructor wants you to pass in them  marks and coursework arrays to a method and the method will return an array of answers. So your for loop that calculates answers will be in a separate method and then the return type of that method will be int[]. Let me know if I need to clarify a little bit, just trying to guide you in the right direction rather than giving you the right answer. So I would first figure out how to pass in two arrays into a method as paramaters, then how to return an array from a method.

you are pretty clear actually, thats exactly what i need to do. but. now i'm confused. because what i had working i cant get it to work anymore. i can just display 2 different arrays with coursework marks and exam marks anymore. this is my code:

 

public class markCalculator 
{
public static void main (String [] args)
{
computeMarks();
}
public static void computeMarks()
{
System.out.println("Exam Mark" + "\t" + "Coursework");
double examMarks [] = {50, 50, 54.6, 50, 50};
double courseworkmark [] = {0, 12, 20.1, 30, 40};
 
for (int i=0; i < examMarks.length; i++)
{
System.out.println(i + "\t" + examMarks);
System.out.println(courseworkmark);
}
 
this is the outcome:.

Exam Mark                   Coursework
0                                50.0
[D@7379567c
1                                50.0
[D@7379567c
2                                54.6
[D@7379567c
3                                50.0
[D@7379567c
4                                50.0
[D@7379567c
 
Link to comment
Share on other sites

Link to post
Share on other sites

 

you are pretty clear actually, thats exactly what i need to do. but. now i'm confused. because what i had working i cant get it to work anymore. i can just display 2 different arrays with coursework marks and exam marks anymore. this is my code:

 

public class markCalculator 
{
public static void main (String [] args)
{
computeMarks();
}
public static void computeMarks()
{
System.out.println("Exam Mark" + "\t" + "Coursework");
double examMarks [] = {50, 50, 54.6, 50, 50};
double courseworkmark [] = {0, 12, 20.1, 30, 40};
 
for (int i=0; i < examMarks.length; i++)
{
System.out.println(i + "\t" + examMarks);
System.out.println(courseworkmark);
}
 
this is the outcome:.

Exam Mark Coursework
0 50.0
[D@7379567c
1 50.0
[D@7379567c
2 54.6
[D@7379567c
3 50.0
[D@7379567c
4 50.0
[D@7379567c
 

 

Same error you had in the other post when trying to print answers. ;)  System.out.println(courseworkmark);

Link to comment
Share on other sites

Link to post
Share on other sites

Same error you had in the other post when trying to print answers. ;)  System.out.println(courseworkmark);

right that is fixed haha, but now, they arent pararrel, ive tried System.out.print (so its not on a new line, but then gets rid of the first array list), can i get a hint or something? haha

Link to comment
Share on other sites

Link to post
Share on other sites

Same error you had in the other post when trying to print answers. ;)  System.out.println(courseworkmark);

i managed to change it to 1 array instead of 2. take a look :)

 

public class tests 
{
public static void main (String [] args)
{
int examMarks [] [] = {{50,40,60,80,70, 11},{65,49,58,77,35,40}};
 
System.out.println ("These are the exam marks and the course work marks");
computeMarks (examMarks);
}
public static void computeMarks(int x[] [])
{
for (int row=0;row<x.length;row++){
for(int column=0; column<x[row].length;column++){
System.out.print (x[row] [column]+"\t");
}
System.out.println();
}
}
}
 
This is the outcome :D
These are the exam marks and the course work marks
50 40 60 80 70 11
65 49 58 77 35 40
 
Link to comment
Share on other sites

Link to post
Share on other sites

anyone can help me with the next part? i have to calculate each elements in the column of that array and get the average of the two and produce the results for each element, in an array.

 

i'll give it a go.



 
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×