Jump to content

Help with Arrays!!! simple program, hopefully simple solution!

- - - - -

  • Please log in to reply
1 reply to this topic

#1
TheChadlyOne

TheChadlyOne

    Newbie

  • Members
  • Pip
  • 1 posts
My assignment reads: II. Write a Java or C program that generates the sum-of-products expansion from a truth table representing a Boolean function in three variables. The variables are named x, y, and z.

The input to the program will consist of eight groups of four Boolean values (0 or 1). Each group represents a row in the truth table defining the Boolean function. As in a truth table, the first three Boolean values in the group are values of x, y, and z; the last Boolean value in the group represents the result of the expression. Input to the program may be read from a file or entered through console prompts.

The output of the program will consist of (1) the resulting truth table containing four columns (x, y, z and F) with appropriate labels, and (2) a sum-of-products expansion of the variables x, y, and z.

I thought it was going well but i cant figure out the arrays and its telling me "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at sumofproducts.SumOfProducts.main(SumOfProducts.java:45)Java Result: 1"

Heres my code:
package sumofproducts;







import java.util.Scanner;

public class SumOfProducts {


    

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int[] xrow= new int[8];

        int[] yrow= new int[8];

        int[] zrow= new int[8];

        int[] output= new int[8];

        int k = 0;

        while (k<8){

            System.out.print("What is x input of line " + (k+1) + "?");

            xrow[k]=in.nextInt();

            System.out.print("What is y input of line " + (k+1) + "?");

            yrow[k]=in.nextInt();

            System.out.print("What is z input of line " + (k+1) + "?");

            zrow[k]=in.nextInt();

            System.out.print("What is output of line " + (k+1) + "?");

            output[k]=in.nextInt();

            k++;

        }

        

        

        System.out.println("X  Y  Z  "+"F(x,y,z)");

        int o = 0;

        while(o<8){

            System.out.println(xrow[o]+"  "+yrow[o]+"  "+zrow[o]+"    "+output[o]);

            o++;

        }

        int u = 0;

        String[] sumofprod = new String[24];


        

        int numofones=0;

        while(u<25){

            if(output[u]==1){

                numofones++;

                if(xrow[u]==1){

                    sumofprod[u]="X";

                }

                if(xrow[u]==0){

                    sumofprod[u]="X'";

                }

                if(yrow[u]==1){

                    sumofprod[u+1]="Y";

                }

                if(yrow[u]==0){

                    sumofprod[u+1]="Y'";

                }

                if(zrow[u]==1){

                    sumofprod[u+2]="Z";

                }

                if(zrow[u]==0){

                    sumofprod[u+2]="Z'";

                }

            } 

            u++;

            u++;

            u++;

            

        }

        

        System.out.print("Sum of Products Expansion:");

        if (numofones>0){

            int m=0;

            while(m<numofones){

                if(m>0){

                   System.out.print(" + ");

                }

            System.out.print(sumofprod[m]+sumofprod[m+1]+sumofprod[m+2]);

            m++;

            }

        }

        else{

            System.out.print("1");

        }

        

        

        

        }

        

    }

Edited by Alyn, 14 October 2011 - 07:48 AM.
added code tag


#2
mastrgamr

mastrgamr

    Newbie

  • Members
  • PipPip
  • 10 posts
Here's what I spotted after a quick glance:

[COLOR=#333333]int numofones=0;[/COLOR]
[COLOR=#333333]while(u<25){[/COLOR]
[COLOR=#333333]if(output[u]==1){[/COLOR]
[COLOR=#333333]numofones++;[/COLOR]
[COLOR=#333333]if(xrow[u]==1){[/COLOR]
[COLOR=#333333]sumofprod[u]="X";[/COLOR]
[COLOR=#333333]}[/COLOR]
[COLOR=#333333]if(xrow[u]==0){[/COLOR]
[COLOR=#333333]sumofprod[u]="X'";[/COLOR]
[COLOR=#333333]}[/COLOR]
[COLOR=#333333]if(yrow[u]==1){[/COLOR]
[COLOR=#333333]sumofprod[u+1]="Y";[/COLOR]
[COLOR=#333333]}[/COLOR]
[COLOR=#333333]if(yrow[u]==0){[/COLOR]
[COLOR=#333333]sumofprod[u+1]="Y'";[/COLOR]
[COLOR=#333333]}[/COLOR]
[COLOR=#333333]if(zrow[u]==1){[/COLOR]
[COLOR=#333333]sumofprod[u+2]="Z";[/COLOR]
[COLOR=#333333]}[/COLOR]
[COLOR=#333333]if(zrow[u]==0){[/COLOR]
[COLOR=#333333]sumofprod[u+2]="Z'";[/COLOR]
[COLOR=#333333]}[/COLOR]

You're looping while 'u' < 25 so when the loop eventually gets above 7, you can't access what's in the Arrays of rows and output you created.

-- Stuart, Currently: Messing with LibGDX (Java)
aspiring video game programmer and college student. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users