Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please answer in c Create a new C project named login _lab02_t02. In the Source Folder (src) of this project create a new C source

please answer in c

Create a new C project named login_lab02_t02. In the Source Folder (src) of this project create a new C source file called "squares_02.c". Copy the contents of the "squares_02.c" file, given below, into the "squares_02.c" source file. Write your code where indicated to reproduce the function of the squares_01.c program of Task-1, above. Other than the output title, the output of these two programs is identical. These two programs differ only in the way the two arrays, namely "values" and "squares", are scanned. That is, how values are stored and retrieved from these arrays. In the squares_01.c program, the arrays are scanned using the ARRAY INDEX method, whereas in the squares_02.c program of Task-2, the arrays are scanned using the POINTER ARITHMETIC method. An brief example of these two array processing methods is given below.

ARRAY INDEXING: =============== int values[10]; // Declare an integer array of 10 values values[5] = 31; // The value of 31 is placed into the 6th array element. printf("Value of the sixth element = [%d] ", values[5]); POINTER ARITHMETIC: ================== int values[10]; // Declare an integer array of 10 values int *pV; // Declare an integer pointer. pV = values; // Pointer pV is set to point to the first element of the array "values". // Another way to do this is: pV = &values[0]; *(pV+5) = 31; // The value of 31 is placed into the 6th array element of the "values" array. printf("Value of the sixth element = [%d] ", *(pV+5)); 

Copy the contents of the "squares_02.c" file, given below, into the "squares_02.c" source file and enter your code where indicated.

Please DO NOT use the array names "values" and "squares" after the indicated point in the program below. Instead, reference these arrays using the "pV" and "pS" pointers. Marks will be awarded only if you use POINTER ARITHMETIC.

/* ------------------------------------------------------------------------------------------ File: squares_02.c Project: heali_lab02_t01 Purpose: Array traversal by "POINTER ARITHMETIC" ======================================= Program Description: This program populates 1D integers arrays with integer values. The "values" 1D array is populated with consecutive integer values in the range [1 to 10]. The "squares" 1D array is populated with the corresponding square values. This version of the program access these two arrays by using the POINTER ARITHMETIC method. ------------------------------------------------------------------------------------------ Author: Heider Ali ID: xxxxxxxxx Email: heali@wlu.ca Version 2020-01-17 Last Updated: 2023-01-20 ------------------------------------- */ #include #include #define MAX_SIZE 10 int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int i; // Loop counter/index. int values [MAX_SIZE]; // Integer Array of size 10. long int squares[MAX_SIZE]; // integer Array of size 10. int *pV; // Integer Pointer. long int *pS; // Long Integer Pointer. pV = values; // or: pV = &values[0]; Set pV to point to first element of array "values". pS = squares; // or: pS = &squares[0]; Set pS to point to first element of array "squares". // === NOTE: From this point on, to the end of the program, you are NOT allowed // === to use or write the "values" or "squares" array names. // === You must reference these arrays ONLY by using the respective pointers "pV" and "pS". // Populate the "values" array with consecutive integers // [1,2,3, ..., 10] <*** your code here ***> { // Populate each element of the "squares" array with the // square value of its corresponding integer value stored in // the "values" array. <*** your code here ***> // Print the table of values printf(" "); printf("Array traversal by \"POINTER INDEXING\" "); printf("===================================== "); printf(" "); printf("Value Square "); printf("===== ========== "); <*** your code here ***> printf("::: "); printf("::: Program Terminated. "); printf("::: "); return 0; } 

The output from this program should look like the following.

Array traversal by "POINTER ARITHMETIC" ======================================= Value Square ===== ========== 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 ::: ::: Program Terminated. :::

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions