Array/File Functions
Write a function to accept three arguments:
The name of a file, a pointer to an int array, and the size of
the array. The
function should open the specified file in binary mode, write the
contents of the
array to the file, and then close the file.
Write another function to accept three arguments:
The name of a file, a pointer to an int array, and the size of
the array. The
function should open the specified file in binary mode, read its
contents into the
array, and then close the file.
Write a complete program that demonstrates these functions:
Function to write an array to a file, and then read the data from
the same file.
An array values shall be created, write to a file, and after that
data are read from
the file, and display the array’s contents on the screen.
Notes:
The filename parameter is the name of the file,
The array parameter is a pointer to the array, and
The size parameter is the number of elements in the array (size
of the array)
In: Computer Science
Random Access Memory (RAM) is _______________ type of memory because the contents are erased when the computer is turned off and is therefore used only for temporary storage while a program is running.
In Python, the % arithmetic operator gives the ______________________ when one integer is divided by another.
_______________ storage is the general name given to storage that can hold data for long periods of time, even when there is no power to the computer.
Python uses ______________________ to categorize values in memory so that it can tell the differences among integers, floating point numbers, and strings .
The binary number for 37 is _____________.
The step in the programming development life-cycle where the task of the program is understood and steps that must be taken to perform the task are determined is ________________.
A(n) __________ decision structure provides only one block of statements as a path to be executed conditionally.
A table that lists Boolean expressions and shows all possible true and false outcomes from compound combinations of the connected Boolean expressions is called a(n) _______________ table.
The general name given to data such as 72, 'A', "Hello", 2.8712 as it appears in a program is ______________
In: Computer Science
Software Engenninring
1. Draw a Level-0, a Level-1, a Level-2 Data Flow Diagram for an ATM Machine System. 2. Draw a sequence diagram for one use-case as follows: Getting money from an ATM Treat each part of the ATM as a class/object: Money dispenser Screen Keypad Bank computer
In: Computer Science
IN JAVA!
Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork.
Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the average number of paintings per artist.
You should create a test class which creates 2 Art Gallery objects, then calls your set methods, get methods, toString and equals methods and average paintings per artist for the Art Gallery objects. MAKE THE PROGRAM ASK USER FOR THE INPUTS ALSO THANKS
In: Computer Science
I am trying to figure out how to properly execute a while loop and a do/while loop in the same program using java. I am stuck at just writing the while loop. I made a condition but I can't figure out how to get it to loop if the condition is true?? Also I have to write a do/while loop in the same program.
here are the details:
Here is what I have written in JAVA so far:
import java.util.Scanner;
public class whileloopanddowhile //BEGIN Class Definition (must be
same as file name)
{
public static void main (String[] args) //BEGIN Main Method
{
//Declare variables
int number;
//List all Class Objects here
Scanner scan = new Scanner(System.in);
//Executable Statement Section, user input
System.out.println ("Enter a grade from 0 to 100 ");
number = scan.nextInt();
//This is where the code goes
while (number < 0 && number > 100 )
{
System.out.println ("ERROR! Must enter a grade between 0 and
100");
}
System.out.println ("You have enetered a valid grade");
}//END main method
}
Thank you for any help and clarification!
In: Computer Science
2. Examine various vulnerabilities of information system that lead to a successful footprinting and recommend the countermeasures for the same.
In: Computer Science
explain why ethical hacking is necessary in today's complex business environment.
In: Computer Science
In: Computer Science
Complete the project functions for this week. These functions are main_menu(), column_full() and get_move(). To complete these functions, first remove the library function call from the function in the template (main_menu_lib, etc), then replace that with your own code to complete the function. You can test your functions separate from the project by creating a small main() function that calls them (with appropriate inputs), or you can compile the entire project and see if it works.main_menu(): This function displays the main menu to the user, which should say "Welcome to the Connect 4 Game", and then prompt the user to ask if they would like to start a new game (press 'n'), load a game (press 'l') or quit (press 'q'). If the user selects new game, the main_menu function should return 1. If they select load game, it should return 2, and if they choose quit it should return -1. If any other input is entered, the user should be prompted to re-enter their choice until a valid input is chosen.column_full(): This function takes the entire board (a COLS x ROWS array of ints) as input as well as a single integer representing the column number to be checked. Note that this input is between 1 and COLS, not0 and COLS-1 so does not correspond exactly to the array dimension.Your function should then check if the specified column is full or not, and return 1 if full, 0 otherwise. The column is full if every element in the column is non-zero.get_move(): This function takes the entire board (a COLS x ROWS array of ints) as input, and then reads a column number from the user, checks that the supplied column is valid (between 1 and COLS), not full (using the column_full() function you've already written) and then returns the valid column number. If the input is invalid or the column is full, an appropriate error message should be displayed and the user asked to enter another column.
TEMPLATE FILE:
#include
#include
#include
#include
#include
#include "connect4.h"
int main ( void ){
int option ;
Game g ;
// intitialise random seed
srand(time(NULL));
while (( option = main_menu()) != -1 ){
if ( option == 1 ){
// setup a new game
setup_game ( &g ) ;
// now play this game
play_game ( &g ) ;
} else if ( option == 2 ){
// attempt to load the game from the save file
if ( load_game ( &g, "game.txt" ) == 0 ){
// if the load went well, resume this game
play_game ( &g ) ;
} else {
printf ( "Loading game failed.\n") ;
}
} else if ( option == -1 ){
printf ( "Exiting game, goodbye!\n") ;
}
}
}
// WEEK 1 TASKS
// main_menu()
// column_full()
// get_move()
// displays the welcome screen and main menu of the game, and prompts the user to enter an option until
// a valid option is entered.
// Returns 1 for new game, 2 for load game, -1 for quit
int main_menu ( void ){
// Dipslay Welcome message
// Continue asking for an option until a valid option (n/l/q) is entered
// if 'n', return 1
// if 'l', return 2
// if 'q', return -1
// if anything else, give error message and ask again..
return main_menu_lib () ;
}
// Returns TRUE if the specified column in the board is completely full
// FALSE otherwise
// col should be between 1 and COLS
int column_full ( int board[COLS][ROWS], int col ){
// check the TOP spot in the specified column (remember column is between 1 and COLS, NOT 0 and COLS-1 so you'll need to modify slightly
// if top spot isn't empty (0 is empty) then the column is full, return 1
// otherwise, return 0
return column_full_lib ( board, col ) ;
}
// prompts the user to enter a move, and checks that it is valid
// for the supplied board and board size
// Returns the column that the user has entered, once it is valid (1-COLS)
// note that this value is betweeen 1 and COLS (7), NOT between 0 and 6!!
// If the user enters 'S' or 's' the value -1 should be returned, indicating that the game should be saved
// If the user enters 'Q' or 'q' the value -2 should be returned, indicating that the game should be abandoned
int get_move ( int board[COLS][ROWS] ){
// repeat until valid input is detected:
// read a line of text from the user
// check if the user has entered either 's' (return -1) or 'q' (return -2)
// if not, read a single number from the inputted line of text using sscanf
// if the column is valid and not full, return that column number
// otherwise, give appropriate error message and loop again
return get_move_lib ( board ) ;
}
// END OF WEEK 1 TASKS
#ifndef CONNECT4_H
#define CONNECT4_H 1
#define ROWS 6 // height of the board
#define COLS 7 // width of the board (values of 9 are going to display poorly!!)
// These lines detect what sort of compiler you are using. This is used to handle the time delay
// function wait() in various operating systems. Most OS will use sleep(), however for windows it is
// Sleep() instead.
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
typedef struct {
int player1, player2 ; // variables for each player - 1 for human, 0 for computer player
int board[COLS][ROWS] ; // the game board. 0 for empty space, 1 for player 1, 2 for player 2
// Note that row 0 is the TOP row of the board, not the bottom!
// column 0 is on the left of the board
int turn ; // whose turn it is, 1 or 2
int winner ; // who has won - 0 for nobody, 1 for player 1, 2 for player 2
} Game ;
// displays the welcome screen and main menu of the game, and prompts the user to enter an option until
// a valid option is entered.
// Returns 1 for new game, 2 for load game, -1 for quit
int main_menu ( void ) ;
// displays the board to the screen
int display_board ( int[COLS][ROWS] ) ;
// sets up the game to a new state
// prompts the user if each player should be a human or computer, and initialises the relevant fields
// of the game structure accordingly
int setup_game ( Game *g ) ;
// Returns TRUE if the specified column in the board is completely full
// FALSE otherwise
// col should be between 1 and COLS
int column_full ( int[COLS][ROWS], int col ) ;
// plays a game until it is over
int play_game ( Game *g ) ;
// prompts the user to enter a move, and checks that it is valid
// for the supplied board and board size
// Returns the column that the user has entered, once it is valid (1-COLS)
// note that this value is betweeen 1 and COLS (7), NOT between 0 and 6!!
// If the user enters 'S' or 's' the value -1 should be returned, indicating that the game should be saved
// If the user enters 'Q' or 'q' the value -2 should be returned, indicating that the game should be abandoned
int get_move ( int[COLS][ROWS] ) ;
// calcualtes a column for the computer to move to, using artificial "intelligence"
// The 'level' argument describes how good the computer is, with higher numbers indicating better play
// 0 indicates very stupid (random) play, 1 is a bit smarter, 2 smarter still, etc..
int computer_move ( int[COLS][ROWS], int colour, int level ) ;
// adds a token of the given value (1 or 2) to the board at the
// given column (col between 1 and COLS inclusive)
// Returns 0 if successful, -1 otherwise
int add_move ( int b[COLS][ROWS], int col, int colour ) ;
// determines who (if anybody) has won. Returns the player id of the
// winner, otherwise 0
int winner ( int[COLS][ROWS] ) ;
// determines if the board is completely full or not
int board_full ( int[COLS][ROWS] ) ;
// saves the game to the specified file. The file is text, with the following format
// player1 player2 turn winner
// board matrix, each row on a separate line
// Example:
//
//1 0 1 0 player 1 human, player 2 computer, player 1's turn, nobody has won
//0 0 0 0 0 0 0 board state - 1 for player 1's moves, 2 for player 2's moves, 0 for empty squares
//0 0 0 0 0 0 0
//0 0 0 2 0 0 0
//0 0 0 2 0 0 0
//0 2 1 1 1 0 0
//0 2 2 1 1 2 1
int save_game ( Game g, char filename[] ) ;
// loads a saved game into the supplied Game structure. Returns 0 if successfully loaded, -1 otherwise.
int load_game ( Game *g, char filename[] ) ;
// waits for s seconds - platform specific! THIS FUNCTION IS INCLUDED IN THE LIBRARY, NO NEED TO WRITE IT!
void wait_seconds ( int s ) ;
// library versions of functions. Exactly the same behaviour done by course staff. Please just call these if you have not completed your version as yet.
int display_board_lib ( int[COLS][ROWS] ) ;
int setup_game_lib ( Game *g ) ;
int column_full_lib ( int[COLS][ROWS], int col ) ;
int play_game_lib ( Game *g ) ;
int get_move_lib ( int[COLS][ROWS] ) ;
int add_move_lib ( int b[COLS][ROWS], int col, int colour ) ;
int winner_lib ( int[COLS][ROWS] ) ;
int board_full_lib ( int[COLS][ROWS] ) ;
int computer_move_lib ( int[COLS][ROWS], int colour, int level ) ;
int save_game_lib ( Game g, char filename[] ) ;
int load_game_lib ( Game *g, char filename[] ) ;
int main_menu_lib ( void ) ;
#endifIn: Computer Science
Topic: Template
template
void arrayContents(const T *arr, int countSize);
int main()
{
const int ACOUNT = 5;
const int BCOUNT = 7;
const int CCOUNT = 6;
int a[ACOUNT] = {1, 2, 3, 4, 5};
double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
char c[CCOUNT] = "HELLO";
cout <<"Array A contents: \n";
arrayContents(a, ACOUNT);
cout <<"Array B contents: \n";
arrayContents(b, BCOUNT);
cout <<"Array C contents: \n";
arrayContents(c, CCOUNT);
return 0;
}
template
void arrayContents(const T *arr, int countSize)
{
for(int i = 0; i < countSize; i++)
cout << arr[i] << " ";
cout << endl;
}
Rewrite the above code and overload function template
arrayContents(), so it will
takes two additional integer arguments, named int lowSubscript and
int
highSubscript. The function shall validate the lowSubscript
and
highSubscript; for:
If both either is out of range, or
If highSubscript is less than or equal to lowSubscript
hence,the overloaded arrayContents function should return 0.
Otherwise, it
should return the number of elements printed.
Modify the main() function to exercise each versions of
arrayContents on arrays
a, b and c.
Notes: the lowSubscript and highSubscript is to identify the index
of array.
In: Computer Science
Identify the main components of an information system. What is a mission-critical system?
software development
In: Computer Science
In: Computer Science
5C++ Questions
1. What will the following code print?
num = 8;
cout << --num << " ";
cout << num++ << " ";
cout << num;
2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop.
3.T/F While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop.
4.How many times will the following program print Hello?
counter = 3;
while (counter > 0) {
loop = 5;
while (loop > 0) {
cout << "Hello" << endl;
loop--;
}
counter--;
}
5.If nothing within a while loop causes the condition to become false, a(n) ________ may occur.
6.The -- operator ... (check all that apply)
|
subtracts one from the value of its operand. |
||
|
must have an lvalue, such as a variable, as its operand. |
||
|
can be used in either prefix or postfix mode. |
||
|
is a unary operator. |
7.Which of the following statements will result in an infinite loop.
x = 10; while (x >= 0) {
cout << x;
++x;
} |
||
x = 10; while (x >= 0) {
cout << x; } |
||
x = 0; while (x <= 10) {
cout << x;
x = 10;
} |
||
x = 10; while (x >= 0); {
cout << x;
x--;
} |
8.T/F In C++, a file must be opened before the contents can be read.
9. T/F In C++, a file must exists before it can be written to.
10.Which of the following statements show the proper way to use the increment (++) operator.
|
x++ = 1023; |
||
|
x = 23++; |
||
|
y = ++x; |
||
|
counter--; |
In: Computer Science
In: Computer Science
<Haskell>
Define an action adder :: IO () that reads a given number of
integers from the keyboard, one per line, and displays their sum.
For example: > adder
How many numbers? 5
1 3 5 7 9
The total is 25
Hint: start by defining an auxiliary function that takes the current total and how many numbers remain to be read as arguments. You will also likely need to use the library functions read and show.
In: Computer Science