Question

Lab # 7 Objectives 1. Ability to use 2-dimensional arrays. 2. Ability to use 1-dimensional Variable...



Lab # 7
Objectives
1. Ability to use 2-dimensional arrays.
2. Ability to use 1-dimensional Variable Length / dynamic arrays.

Part 1: 2-Dimensional int array

Write and execute the following code to create a 1-dimensional int array.

#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/Counter variables for the loop/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}

Part 2: Array initialization
In the example above you required the user to input the values for the array.
Modify the program in Part 1 so that instead of asking the user for the values of array elements, they are all initialized using a single statement.

Part 3: Dynamic/Variable Length 1-dimensional array
Write a program that does the following:
1. Asks user for a positive integer, N.
2. Creates a 1-dimensional integer array of length N.
3. Stores the squares of all integers from 1 to N in the array.
4. Prints out all N values of the array.

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

In these C programs:

- For part 1, we have created a 1D int array accepting input from the user to store the values in the array and then printing it.

- For part 2, we have created 2D int array and printing it

- For part 3, we have created Dynamic/Variable-Length 1D array and printing it.

Part 1: Creating a 1-Dimensional int array:

#include<stdio.h>
int main()
{
/* 1D array declaration*/
int disp[2];
  
//Counter variable for the loop
int i;
  
//Accepting the input values for the 1D array
for(i=0; i<2; i++)
{
printf("Enter value for disp[%d]:", i);
scanf("%d", &disp[i]);
}
  
//Displaying array elements
printf("One Dimensional array elements:\n");
for(i=0; i<2; i++)
{
printf("%d ", disp[i]);
  
}
return 0;
}

Output:

Part 2: 2D Array initialization:

#include<stdio.h>
int main()
{
/* 2D array declaration*/
int disp[2][3]={1,2,3,4,5,6};
  
//Counter variables for the loop
int i, j;

//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++)
{
for(j=0;j<3;j++)
{
printf("%d ", disp[i][j]);
if(j==2)
{
printf("\n");
}
}
}
return 0;
}

Output:

Part 3: Dynamic/Variable-Length 1-dimensional array

#include<stdio.h>
int main()
{
//declaring a positive integer N and reading value
int N;
printf("Please enter a Positive integer: ");
scanf("%d",&N);
  
//1D array declaration of length N
int disp[N];
  
//Counter variable for the loop
int i;
  
for(i=1;i<=N;i++)
{
disp[i-1]=i*i;   
}

//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<N; i++)
{
printf("%d ", disp[i]);
}
return 0;
}

Output:

Hope this Helps!!!

If not please comment, I will Help you with that..

Add a comment
Know the answer?
Add Answer to:
Lab # 7 Objectives 1. Ability to use 2-dimensional arrays. 2. Ability to use 1-dimensional Variable...
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
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