Thread: Assistance with some homework, please?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    as I understand your problem is that you can't realy access the arrays from the function?

    You can solve this by making the arrays global by declaring them outside of the main function. Get my drift?

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    try this:

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    void genLife();
    void showLife();
    int x;
    int y;
    int max;
    char choice;
    int cell[20][20];
    int main(int argc, char *argv[])
    {
    max = 20;
    for(int row=0;row < max;row++){
    
       for(int colom=0;colom < max;colom++){
       cell[row][colom] = 0;
       }
    }
    cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl;
    
    do {
    cout << "Enter coordiantes:\n";
    cin >> x >> y;
    if(x > max || y > max){
    cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n";
    cin >> x >> y;
    }else{
    cell[x][y] = 1;
    }
    }
    while(x >= 0 && y >= 0);
    showLife();
    return 0;
    }
    
    
    void showLife(){
    int colom = 0;
    int row = 0;
    do {
    cout << "[" << cell[row][colom] << "]";
    colom = colom + 1;
    if (colom == max){
    row = row + 1;
    colom = 0;
    cout << endl;
    }
    }
    while(row < max && colom < max);
    
    cout << "\n\nGo to next generation? (Y/N)" << endl;
    cin >> choice;
    if(choice == 'y' || choice == 'Y'){
    genLife();
    }else{
    int life = 0;
       for(int colom=0;colom < max;colom++){
       if(cell[row][colom] == 1){
       life = life +1;
       }
    }
    
    cout << "Thank you for using my world simulator" << endl;
    }
    }
    
    void genLife(){
    int colom = 0;
    int row = 0;
    int c = 0;
    do {
    if(cell[row][colom + 1] == 1){
    c = c +1;
    }
    if(cell[row][colom - 1] == 1){
    c = c + 1;
    }
    if(cell[row +1][colom] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom +1] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom -1] == 1){
    c = c + 1;
    }
    if(cell[row + 1][colom -1] == 1){
    c = c + 1;
    }
    if(cell[row + 1][colom +1] == 1){
    c = c + 1;
    }
    if (c == 3 || c == 2 && cell[row][colom] == 1){
    cell[row][colom] = 1;
    }else{
    cell[row][colom] = 0;
    }
    if (c == 3 && cell[row][colom] == 0){
    cell[row][colom] = 1;
    }
    c = 0;
    colom = colom + 1;
    if (colom == max){
    row = row + 1;
    colom = 0;
    }
    }
    while(row < max && colom < max);
    showLife();
    }

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    try this:

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    void genLife();
    void showLife();
    int x;
    int y;
    int max;
    char choice;
    int cell[20][20];
    int main(int argc, char *argv[])
    {
    max = 20;
    for(int row=0;row < max;row++){
    
       for(int colom=0;colom < max;colom++){
       cell[row][colom] = 0;
       }
    }
    cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl;
    
    do {
    cout << "Enter coordiantes:\n";
    cin >> x >> y;
    if(x > max || y > max){
    cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n";
    cin >> x >> y;
    }else{
    cell[x][y] = 1;
    }
    }
    while(x >= 0 && y >= 0);
    showLife();
    return 0;
    }
    
    
    void showLife(){
    int colom = 0;
    int row = 0;
    do {
    cout << "[" << cell[row][colom] << "]";
    colom = colom + 1;
    if (colom == max){
    row = row + 1;
    colom = 0;
    cout << endl;
    }
    }
    while(row < max && colom < max);
    
    cout << "\n\nGo to next generation? (Y/N)" << endl;
    cin >> choice;
    if(choice == 'y' || choice == 'Y'){
    genLife();
    }else{
    int life = 0;
       for(int colom=0;colom < max;colom++){
       if(cell[row][colom] == 1){
       life = life +1;
       }
    }
    
    cout << "Thank you for using my world simulator" << endl;
    }
    }
    
    void genLife(){
    int colom = 0;
    int row = 0;
    int c = 0;
    do {
    if(cell[row][colom + 1] == 1){
    c = c +1;
    }
    if(cell[row][colom - 1] == 1){
    c = c + 1;
    }
    if(cell[row +1][colom] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom +1] == 1){
    c = c + 1;
    }
    if(cell[row - 1][colom -1] == 1){
    c = c + 1;
    }
    if(cell[row + 1][colom -1] == 1){
    c = c + 1;
    }
    if(cell[row + 1][colom +1] == 1){
    c = c + 1;
    }
    if (c == 3 || c == 2 && cell[row][colom] == 1){
    cell[row][colom] = 1;
    }else{
    cell[row][colom] = 0;
    }
    if (c == 3 && cell[row][colom] == 0){
    cell[row][colom] = 1;
    }
    c = 0;
    colom = colom + 1;
    if (colom == max){
    row = row + 1;
    colom = 0;
    }
    }
    while(row < max && colom < max);
    showLife();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM