##### arraylist

 Programming Tutor: None Selected Time limit: 9 Hours

# 1.Exercise : printStrings

Write a method called `printStrings` that takes as a parameter a `Scanner` holding a sequence of integer/string pairs and that prints to `System.out` one line of output for each pair with the given string repeated the given number of times.

# 2.Exercise : isAllEven

Write a method called `isAllEven` that takes an array of integers as a parameter and that returns whether or not all of the values are even numbers (`true` for yes, `false` for no).

# 3.Exercise : longestSortedSequence

Write a method called `longestSortedSequence` that accepts an array of integers as a parameter and that returns the length of the longest sorted (nondecreasing) sequence of integers in the array.

Nov 20th, 2013
increase the pledge
Nov 20th, 2013

i am about to fish the 2 part

Nov 20th, 2013

`#include <iostream>#include <vector>#include <tr1/memory>#include <algorithm>#include <iterator> template <typename E>struct Node {  E value;  std::tr1::shared_ptr<Node<E> > pointer;}; template <class E>struct node_ptr_less {  bool operator()(const std::tr1::shared_ptr<Node<E> > &node1,		  const std::tr1::shared_ptr<Node<E> > &node2) const {    return node1->value < node2->value;  }};  template <typename E>std::vector<E> lis(const std::vector<E> &n) {  typedef std::tr1::shared_ptr<Node<E> > NodePtr;   std::vector<NodePtr> pileTops;  // sort into piles  for (typename std::vector<E>::const_iterator it = n.begin(); it != n.end(); it++) {    NodePtr node(new Node<E>());    node->value = *it;    typename std::vector<NodePtr>::iterator j =      std::lower_bound(pileTops.begin(), pileTops.end(), node, node_ptr_less<E>());    if (j != pileTops.begin())      node->pointer = *(j-1);    if (j != pileTops.end())      *j = node;    else      pileTops.push_back(node);  }  // extract LIS from piles  std::vector<E> result;  for (NodePtr node = pileTops.back(); node != NULL; node = node->pointer)    result.push_back(node->value);  std::reverse(result.begin(), result.end());  return result;} int main() {  int arr1[] = {3,2,6,4,5,1};  std::vector<int> vec1(arr1, arr1 + sizeof(arr1)/sizeof(*arr1));  std::vector<int> result1 = lis(vec1);  std::copy(result1.begin(), result1.end(), std::ostream_iterator<int>(std::cout, ", "));  std::cout << std::endl;   int arr2[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};  std::vector<int> vec2(arr2, arr2 + sizeof(arr2)/sizeof(*arr2));  std::vector<int> result2 = lis(vec2);  std::copy(result2.begin(), result2.end(), std::ostream_iterator<int>(std::cout, ", "));  std::cout << std::endl;  return 0;}`

Nov 20th, 2013

above is part 3

Nov 20th, 2013

is that java?

Nov 20th, 2013

yes you nedd in c++

Nov 20th, 2013

Sub-Categories:`Java `

Nov 20th, 2013

``````Question 2

#include <iostream>
using namespace std;
int main()

{
int input, remainder, even = 0, odd = 0;
int evenArray[even];
cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
cin >> input;
while (input != 0)

{
remainder = input % 2;
if (remainder == 0)

{
evenArray[even] = input;
even++;
}
else

{
odd++;
}
cout << "Enter another integer: ";
cin >> input;
}

if

{
odd!-0;

cout<<"False"<<endl;

}
else

{

cout<<"True"<<endl;

}``````

Nov 20th, 2013

it looks pretty good i am trying to get it to work with my main class. part 2 ?

Nov 20th, 2013

ok now going for third one. . .

Nov 20th, 2013

but its not java. i need it to be java

Nov 20th, 2013

ok wait

Nov 20th, 2013

import java.util.*;

import java.io.*;

import java.util.Collections;

public class App{

public static void main(String[] args)

throws FileNotFoundException{

Scanner in1 = new Scanner(new File("name.txt"));

Scanner in2 = new Scanner(new File("number.txt"));

ArrayList<String> list1 = printStrings(in1);

ArrayList<Integer> list2 = isAllEven(in2);

System.out.println(list1);

System.out.println(list2);

}

public static ArrayList<String> printStrings(Scanner input){

ArrayList<String> strings = new ArrayList<String>();

while (input.hasNext()){

String next = input.next();

}

return strings;

}

public static ArrayList<Integer> isAllEven(Scanner input){

ArrayList<Integer> number = new ArrayList<Integer>();

for(int i=100;i<=200;i++){

if(i%2==0){

System.out.println(i);

}

}

return number();

}

}

this is what i got so far

Nov 20th, 2013

`this is accurate check it//import Scanner as we require it.``import` `java.util.Scanner;   `

`// the name of our class its public`

`public` `class` `ArrayEvenNumber `

`{`

`//void main`

`public` `static` `void` `main (String[] args)`

`{`

`//declare int`

`int` `a=0,i,x,a[] = ``new` `int``[``10``];`

`//Declare input as scanner`

`Scanner input = ``new` `Scanner(System.in);`

`//Take input for arrays`

`for``(i=``0``;i<``5``;i++)`

`{`

`System.out.println(``"Enter Number :"``);`

`a[i] = input.nextInt();`

`}`

`system.out.println(``"Even Numbers are :-"``);`

`//print the even numbers`

`for``(i=``0``;i<``5``;i++)`

`{`

`x=a[i]%``2``;`

`if``(x==``0``)`

`System.out.println(a[i]);`

`else`

`a=a+1`

`}`

`if a!=0;`

`System.out.println(False);`

`else`

`System.out.println(True);`

`}`

`}`

Nov 20th, 2013

it shows too many errors

Nov 20th, 2013

import java.util.*;

import java.io.*;

import java.io.*;

public class App{

public static void main(String[] args)

throws FileNotFoundException{

Scanner in1 = new Scanner(new File("name.txt"));

ArrayList<String> list1 = printStrings(in1);

System.out.println(list1);

int [] list2 = {6, 8, 10, 218, 28, 88, 68, 98, 206, 1674, 3424};

int [] list3 = {6, 88, 68, 8, 1670, 28098, 3416, 37, 92, 1748};

System.out.println("List 2 " + (isAllEven(list2)? "is" : "is not") + " all even");

System.out.println("List 3 " + (isAllEven(list3)? "is" : "is not") + " all even");

}

public static ArrayList<String> printStrings(Scanner input){

ArrayList<String> strings = new ArrayList<String>();

while (input.hasNext()){

String next = input.next();

}

return strings;

}

public void isAllEven (){

int [] list1 = {18, 0, 4, 204, 8, 4, 2, 18, 206, 1492, 42};

int [] list2 = {2, 4, 6, 8, 10, 208, 16, 7, 92, 14};

System.out.println("List1 " + (isAllEven(list1)? "is" : "is not") + " all even");

System.out.println("List2 " + (isAllEven(list2)? "is" : "is not") + " all even");

}

public static boolean isAllEven (int[] p) {

for (int i = 0 ; i < p.length ; i++) {

if (p[i] % 2 == 1)

return false;

}

return true;

}

}

this is what i have so far

Nov 20th, 2013

...
Nov 20th, 2013
...
Nov 20th, 2013
May 22nd, 2017
check_circle