C Exercises: Array fill, replace each subsequent position of the array by one-third value of the previous
C Basic Declarations and Expressions: Exercise-129 with Solution
Write a C program that reads an array (length 10), and replace the first element of the array by a give number and replace each subsequent position of the array by one-third value of the previous.
Sample Solution:
C Code:
#include <stdio.h>
int main ()
{
int i;
double array_nums[10];
double n;
printf("Input a number:\n");
scanf("%lf", &n);
array_nums[0] = n;
for (i = 1; i < 10; i++)
{
n = n/3;
array_nums[i] = n;
}
printf("\nArray elements:\n");
for (i = 0; i < 10; i++)
{
printf("array_nums[%d] = %.4lf\n", i, array_nums[i]);
}
}
Sample Output:
Input a number: 35 Array elements: array_nums[0] = 35.0000 array_nums[1] = 11.6667 array_nums[2] = 3.8889 array_nums[3] = 1.2963 array_nums[4] = 0.4321 array_nums[5] = 0.1440 array_nums[6] = 0.0480 array_nums[7] = 0.0160 array_nums[8] = 0.0053 array_nums[9] = 0.0018
Flowchart:
C programming Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C program that reads an array of integers (length 10), fill the array elements with number o to a (given number) n – 1 repeated times where 2 <= n <= 10.
Next: Write a C program to create an array of length n and fill the array elements with integer values. Now find the smallest value and it’s position within the array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework