Question

You have an array that is defined as int array2[4] = {50, 100, 150, 200}; write...

You have an array that is defined as int array2[4] = {50, 100, 150, 200};

write a for loop and a while loop to print each element of the array with a label in the following manner using printf() with the correct format string.

array2[0] = 50

array2[1] = 100

array2[2] = 150

array2[3] = 200


can it be written in C
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Using For Loop

#include<stdio.h>
int main() {
int array2[4]={50,100,150,200};
int i;
for(i=0;i<4;i++)
{
printf("array2[%d] = %d\n",i,array2[i]);
}
return 0;
}

//Using While Loop

#include<stdio.h>
int main() {
int array2[4]={50,100,150,200};
int i;
while(i<4)
{
printf("array2[%d] = %d\n",i++,array2[i]);
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
You have an array that is defined as int array2[4] = {50, 100, 150, 200}; write...
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
  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

  • Write MIPS code for the highlighted part # Assume that factorial is defined as factorial(int x)...

    Write MIPS code for the highlighted part # Assume that factorial is defined as factorial(int x) .data num: .word 8 large_str: .asciiz “Factorial result is large” small_str: .asciiz .text # int y = factorial(num); lw $t0, num # push this onto the stack before calling factorial(int) ____________ ____________ ____________ # clean up the stack after wards ____________ # You can use the print string syscall in lieu of a call to printf # if(y > 200) { printf(large_str); } addi...

  • Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize...

    Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize your array using a text file, user input or assign value while declaring the array, you can choose the method. Then, a. Write a loop to print first row of the array on one line, using cout. b. Write a loop to print first column of the array on one line, using cout. c. Write a loop to print first five rows of the...

  • C - Language Write a loop that sets each array element to the sum of itself...

    C - Language Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 +...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • 7.2.3: Printing array elements with a for loop. Write a for loop to print all elements...

    7.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) Also note: If the submitted code tries to access an invalid...

  • I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small...

    I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small progressive parts, with the intention of developing the skill of working with arrays. Do each part separately. Include in your submission the code and output for Part I, then the code and output for Part 2, etc. into a Word document. Specification: Part 1. Write a main function that declares an array of 10 int’s. Assign each element in the array a value between...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement...

    Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement to set bPtr equal to the address of the first element in array b. Write a statement using pointer expression to reference the array element b[3]. please make sure answer should be detailed ,  correct and in C language.

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