Question

In: Computer Science

Lab 7: 2D Arrays Project Goals The goals of this project are to: 1.       Get students...

Lab 7: 2D Arrays

Project Goals

The goals of this project are to:

1.       Get students familiar with 2D arrays

2.       Continue loop practice

Important Notes:

1.        Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text of the problem from “Enter the number of rows in your array: ” to “Enter the number of rows: ”. Your assignment will be auto-graded and any change in formatting will result in a loss in the grade.

2.       Comments: Header comments are required on all files and recommended for the rest of the program. Points will be deducted if no header comments are included.

Problem 1

Save your program as squaring.c

It’s better to be odd, so let’s square those values. Write a program which gets values into an array and then squares the odd values. The program should prompt the user to enter the number of rows -1[1] and columns they would like in an array. Then it should prompt the user to enter values to fill that array. It should iterate over all the values in the array and square any which are odd. Display those values back to the user.

The program should function as follows (items underlined are to be entered by the user):

Enter the number of rows in your 2D array: 2

Enter the number of columns in your 2D array: 2

Enter a value to save: 1

Enter a value to save: 2

Enter a value to save: 3

Enter a value to save: 4

1 2

9 4

Run your program again, testing it with different values.

Notes:

  • You know the range and step, which loop is best?
  • Know which variables to initialize!

[1]I think this should be ...rows... and not ...rows-1...

Solutions

Expert Solution

Save your program as squaring.c. Hence, the required programming language is C.

C Program:

#include <stdio.h>

int main()
{
int rows,cols,i,j;
printf("Enter the number of rows in your 2D array: ");
scanf("%d",&rows);
printf("Enter the number of columns in your 2D array: ");
scanf("%d",&cols);
int arr[rows][cols];
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
printf("Enter a value to save: ");
scanf("%d",&arr[i][j]);
if(arr[i][j]%2!=0)
arr[i][j]=arr[i][j] * arr[i][j];
}
}
  
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}

Output:


Related Solutions

Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
Project [2]: Expressions and Operators Project Goals The goal of this project is to: 1. Get...
Project [2]: Expressions and Operators Project Goals The goal of this project is to: 1. Get students familiar with expressions and operators. Important Notes: 1. Formatting: Make sure that you follow the precise recommendations for the output content and formatting. For example, do not change the text from “You’re walking through the woods and come upon a chest. Do you open it? 1 – yes 2 - no” to “Do you open the chest? ”. Your assignment will be auto-graded...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of their VMs and running wireshark on their VMs. We also examine Ethernet, IPv4, and TCP addressing at the Network Access, Network, and Transport layers of the TCP/IP stack. Reflection: In two paragraphs reflect the experience of using Wireshark capture (in the lab) on the following questions: What was the most valuable feature of the lab? How did you prepare for this lab? What changes...
Must be written in C: Lab 3: Logical Expressions and if Statements Project Goals The goals...
Must be written in C: Lab 3: Logical Expressions and if Statements Project Goals The goals of this project are to: 1. Get students familiar with evaluating logical expressions 2. Get students familiar with if - else statements Important Notes: 1. Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text of the problem from “ Enter the number of loaves of bread and price per loaf: ”...
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle...
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle /* OUTPUT: Enter the width of the court: 60 Enter the length of the court: 120 The width of the court is 60 feet. The length of the court is 120 feet. The area of the court is 7200 square feet. Press any key to continue . . . */ 2.) Create the following 3 files: Rectangle.h Rectangle.cpp Source.cpp 3.) Write a program that...
7. If 35% of students get refund on their tax return and we ask 25 students...
7. If 35% of students get refund on their tax return and we ask 25 students whether they got refunds on their tax returns or not. a. Define the random variable X b. List the values X can take on. c. Give the distribution of X. X __(______,______) d. How many of the 25 students would we expect to get refunds?
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the...
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the results in n0 and n1: for (i=0; i<MAX_ROW; i++)   for (j=0; j<MAX_COL;j++){        //calculate number of zeros around entry a[i][j]        n0[i][j] =        //calculate number of ones around entry a[i][j]        n1[i][j] =   } Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line inputs as in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT