Question

IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will hold the pheromone levels between adjacent ant colonies. o Declare a 1-dimensional array of size the global integer constant, SIZE that will hold the colonies visited in the order in which they were visited. This array will hold the row indices from the 2-dimensional array. If Anthony visits a colony, store the integral value of that colony (0 for A, 1 for B, 2 for C and so on) in the 1-dimensional array. For example, if Anthony visits index A first, the first element in the 1-dimensional array would be 0. If Anthony visits colony C next, the second element in the array would be 2 and so on. • Declare and define a function to initialize the 2-dimensional int array declared in the main function such that: o It accepts a two-dimensional int array as a parameter. o It doesn’t return a value. o Within this function, generate seeded random numbers between 1 and 20 (inclusive) and assign these values in a row-major format (i.e., fill an entire row with random values before moving to the next row). Do this as long as the row and column number are not equal. If they are equal, assign a value of 0 to that node (because we assume that there is no path from an ant colony to itself). o You should end up with a matrix where the diagonal elements are 0. Please see the sample output. • Declare and define a function to print the contents of the two-dimensional array generated in the previous step. o It should take the previously generated two-dimensional int array as its parameter. o It should not return a value. o Please see the sample output for the format of the output. • Declare and define a function that checks if a particular colony was visited before. o It takes two parameters: the 1-dimensional array that holds the colonies visited and an integer that represents a colony. o It returns a boolean value; true if the colony (represented by the integer) was visited before or false otherwise. o To determine if a colony has been visited before, you will have to iterate over all the values in the 1-dimensional array (that holds all colonies previously visited by Anthony) and check to see if the integer parameter is present in it. • Declare and define a function that computes the index where the lowest (non-zero) pheromone levels are found in a given row. o It takes three parameters; the 2-dimensional array declared in the main function that contains all the pheromone levels, the 1-dimensional array that contains the list of all the colonies that were visited and an integer that represents a row in the 2-dimensional array. o For each of the columns in a row, compute the lowest non-zero value only if that colony wasn’t visited before. To check if a colony was visited before, you must call the function defined in the previous step. o For example, in the sample output, when Anthony first starts his journey at colony A, he has to find the path with lowest, non-zero pheromone levels in that row (row 0), which is 4 at column 6 that represents colony G. Anthony then travels from colony A to colony G. • Declare and define a function that prints the contents the array that holds the colonies visited. o This function will take the 1-dimensional array as a parameter. o It will not return a value. o Using a loop of your choice, process each element stored in the 1-dimensional array like so: ▪ Use a switch statement and each of the enum values as case constants, print the path that Anthony took between the ant colonies. For example, for case A, print “A - - - - ->”. Do this for the rest of the cases. Keep in mind, index 0 means colony ‘A’, index 1 is colony ‘B’ and so on until colony ‘J’. ▪ Please make sure that your output is well formatted. You may use the printf instruction with space formatting to ensure that all elements of the matrix are aligned (like the sample output). Additionally, use the precision format specifier in the printf statement to ensure that each integer is represented using two digits. • Inside your main function you will need to: o Call the function that prints the student information. o Call the function that prints a brief description of what his project aims to do. o Call the function that initializes the 2-dimensional array with random numbers. Don’t forget to provide the 2-dimensional array as a parameter to this function. o Call the function that prints the contents of the 2-dimensional array. Again, don’t forget to provide the 2-dimensional array as a parameter during this function call. o Anthony always starts his journey from colony ‘A’. So, mark this colony as visited. o Starting with colony A, he must travel to the colony with the lowest non-zero pheromone levels, provided that it wasn’t visited before. This means that the rows in the array are accessed non-sequentially, but all the columns in a given row are always accessed sequentially to determine the lowest pheromone levels. o Once you obtain the column index where the lowest (non-zero) pheromone level is found in a given row, you must go to the row that is represented by that column index and continue this process. Don’t forget to mark this colony as visited. o To determine the lowest pheromone levels in a given row, call the function that returns the index at which the lowest value is found. Don’t forget to provide the 2-dimensional array, the 1- dimensional array and the row currently being accessed as parameters to this function. o If the colony with the lowest pheromone levels was visited before, disregard that colony and proceed to the colony with the next lowest pheromone levels. o If the pheromone levels between two or more colonies is equal, you are free to arbitrarily choose one of those colonies, provided of course that colony hasn’t been visited before. o Anthony can only visit a colony once. Once a colony is visited, Anthony cannot return to it. So, keep track of the colonies visited. So, make sure that all values in the 1-dimensional array are unique. o He must visit each of the 10 colonies (A through J). o If Anthony visits a colony, store the integral value of that colony (0 for A, 1 for B, 2 for C and so on) in the 1-dimensional array. For example, if Anthony visits index A first, the first element in the 1-dimensional array would be 0. If Anthony visits colony C next, the second element in the array would be 2 and so on. o In addition to the colonies visited, you must also keep track of the order in which they were visited. • Finally, display the path that Anthony took starting from colony A using the function that prints the contents of the 1-dimensional array. • Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name, course section, date, and brief description), comments for each variable, commented blocks of code, and function header comments. • Your program source code should be named “euidHW3.cpp”, without the quotes. • Your program will be graded based largely on whether it works correctly on the CSE machines (e.g., cse01, cse02, …, cse06), so you should make sure that your program compiles and runs on a CSE machine. • You should contact your instructor if there is any question about what is being asked for. You may assume that all input will be of the appropriate data type, although the range (e.g., an integer) may be out of bounds of the region. Please pay attention to the SAMPLE OUTPUT for specific details about the flow and input/output of the program. You shall use techniques and concepts discussed in class – you are not to use global variables (other than SIZE and enum), goto statements, or other items specifically not recommended in this class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>               //For basic I/O
#include<stdlib.h>               //For rand() and srand() functions
#define SIZE 10
using namespace std;


enum E{A, B, C, D, E, F, G, H, I, J};

void initialize(int arr[][SIZE])
{
   for(int i = 0;i<SIZE;i++)
       for(int j=0;j<SIZE;j++)
       {
           if(i==j)
           {
               arr[i][j] = 0;
               continue;
           }
           srand(rand()%100+1000+i+j+rand());
           arr[i][j] = rand()%20 + 1;
           srand(i+j);
       }
}

void print(int arr[][SIZE])
{
   cout<<endl;
   for(int i=0;i<SIZE;i++)
   {
       for(int j=0;j<SIZE;j++)
       {
           cout<<arr[i][j]<<"\t";
       }
       cout<<endl;
   }
}

bool isVisited(int *arr, int val)
{
   for(int i=0;i<SIZE;i++)
       if(arr[i]==val)
           return true;
   return false;
}

int lowestpheromoneindex(int arr1[][10],int *arr2, int row)
{
   int min=arr1[row][0];
   int index = 0;
   if(!isVisited(arr2,row))
   {
       for(int i = 1;i<SIZE;i++)
       {
           if(i!=row)
               if(arr1[row][i]<min)
               {
                   min = arr1[row][i];
                   index = i;
               }
       }
       return index;
   }
   return -1;
}

void displayVisited(int *arr)
{
   int temp;
   for(int i=0;i<SIZE;i++)
   {
       temp = arr[i];
       switch(temp)
       {
           case A: cout<<"A ----->";
               break;
           case B: cout<<"B ----->";
               break;
           case C: cout<<"C ----->";
               break;
           case D: cout<<"D ----->";
               break;
           case E: cout<<"E ----->";
               break;
           case F: cout<<"F ----->";
               break;
           case G: cout<<"G ----->";
               break;
           case H: cout<<"H ----->";
               break;
           case I: cout<<"I ----->";
               break;
           case J: cout<<"J ----->";
               break;
       }
   }
}

int main()
{
   int arr[SIZE][SIZE];
   int visited[SIZE];
   //printStudentInformation();
   //printDescription();
   initialize(arr);
   print(arr);
   visited[0] = 0;
   int i = 1;
   int minIndex = 0;
   while(i<10)
   {
       minIndex = lowestpheromoneindex(arr,visited,minIndex);
       if(!isVisited(visited,minIndex))
       {
           visited[i] = minIndex;
           i++;
       }

   }
   displayVisited(visited);
   return 0;
}

//According to the question solution is designed.

#Note: There is some discrepency in the question with that it may go to infinite loop if rand() is not generating uniqely. Consult you instructor for that.

#Please leave your rating. Hope you like it! Ask doubts if any in the comments section.

Add a comment
Know the answer?
Add Answer to:
IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use C Programming DESCRIPTION Write a program that: Creates a Dimensional, integer array that holds 8...

    Use C Programming DESCRIPTION Write a program that: Creates a Dimensional, integer array that holds 8 values Prompts the user to enter 8 integers between 0 and 20 [inclusive) and stores the data in the array Prints the data in array abng with a histogram of the data as shown below. There is no requirement to validate the data. You must use a constant to declare your array and control any loops you need Sample Output: Enter 8 integer values...

  • Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFilename and MapFilename. Use char...

    Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFilename and MapFilename. Use char arrays and set them to nulls by using {}. Create a structure called RowCol that has two int members called row and col to hold the row and column. Function MoveNorth should have a return value of void and a parameter of a pointer to RowCol. { To move north, you need to subtract 1 from PositionPtr->row. Before doing so, you need to check...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Please solve this C language progrm (2-D Array program) the most basic way ! Thank You!...

    Please solve this C language progrm (2-D Array program) the most basic way ! Thank You! Write a function that takes a 4 x 4 array of integers as its parameter. The function must use two nested for-loops to determine the largest integer in the array as well as its row and column numbers. Recall that C-functions cannot return multiple values. Use pointers in the function parameter list to relay the results to the caller program. Write another function, which...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Please Write in Java 15 salesmen have performed sales in 10 different cities and the information...

    Please Write in Java 15 salesmen have performed sales in 10 different cities and the information is stored in a two-dimensional array. For example: City 0 City 1 City 2 City … City 9 salesman 0 3.4 4.0 2.6 …… 3.5 salesman 1 3.7 4.44 1.90 …… 5.9 salesman … 1.5 5.9 7.4 …… 0.0 salesman 14 44.4 5.6 7.8 ….. 0.9 Write a class called Employee that contains the following methods A static method called totalSales() (String name, double...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT