Question

(a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called output.dat. Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down the C code to: (i) declare an array called ivec consisting of 10 integers; (ii) dynamically allocate memory for an array called fvec consisting of n floating-point numbers, where the value of n is input at the keyboard; (iii) set the first element of fvec to be equal to 2.7; (iv) assign values to the remaining elements of fvec so that every such element is equal to the cube of the previous element; (v) print the final element of fvec to the screen, using 2 decimal places. Explain clearly the difference between an array and a character string. (40%)

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

a)

#include<stdio.h>
#include <stdlib.h>

// main function definition
int main()
{
// Loop variable
int c;
// File pointer declared
FILE *fptr;
// Open the file for writing
fptr = fopen("output.dat", "w");
// Checks if fptr is equals to NULL then file cannot be opened
if(fptr == NULL)
{
// Displays error message
printf("Error: Unable to open the file");
// Exit the program
exit(1);
}// End of if condition

// Displays the heading
printf("%s \n", "Number divisible by 7 but not by 13");
// Writes the heading to file
fprintf(fptr,"%s \n", "Number divisible by 7 but not by 13");
// Loops from 1 to 1000
for(c = 1; c < 1000; c++)
{
// Checks if c is divisible by 7 and c is not divisible by 13
if((c % 7 == 0) && (c % 13 != 0))
{
// Displays the number
printf("%d \t", c);
// Write the number to file
fprintf(fptr,"%d \t", c);
}// End of if condition
}// End of for loop
// Close the file
fclose(fptr);
}// End of main function

Sample Run:

Number divisible by 7 but not by 13
7 14 21 28 35 42 49 56 63 70 77 84 98 105 112
119 126 133 140 147 154 161 168 175 189 196 203 210 217 224
231 238 245 252 259 266 280 287 294 301 308 315 322 329 336
343 350 357 371 378 385 392 399 406 413 420 427 434 441 448
462 469 476 483 490 497 504 511 518 525 532 539 553 560 567
574 581 588 595 602 609 616 623 630 644 651 658 665 672 679
686 693 700 707 714 721 735 742 749 756 763 770 777 784 791
798 805 812 826 833 840 847 854 861 868 875 882 889 896 903
917 924 931 938 945 952 959 966 973 980 987 994

File Name: output.dat contents

Number divisible by 7 but not by 13

7 14 21 28 35 42 49 56 63 70 77 84 98 105 112 119 126 133 140 147 154 161 168 175 189 196 203 210 217 224 231 238 245 252 259 266 280 287 294 301 308 315 322 329 336 343 350 357 371 378 385 392 399 406 413 420 427 434 441 448 462 469 476 483 490 497 504 511 518 525 532 539 553 560 567 574 581 588 595 602 609 616 623 630 644 651 658 665 672 679 686 693 700 707 714 721 735 742 749 756 763 770 777 784 791 798 805 812 826 833 840 847 854 861 868 875 882 889 896 903 917 924 931 938 945 952 959 966 973 980 987 994

b)

C Pre-processor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation.

Source code is processed by a program called pre-processor. This process is called pre-processing.

All pre-processor commands begin with a hash symbol (#).

1) Macro: macro defines constant value and can be any of the basic data types.

Generally pre processor macro names are in capital letter to distinguish it from user defined macro name.

Syntax: #define macro_name macro_operation
Example: #define MAX 100

2) Header File: The source code of the file “file name” is included in the main program.

Syntax: #include filename

Example:

#include <stdio.h>          à Predefined header file for i/o operation

#include "myHeader.h" à User defined header file

#define SQUARE(x)   (x) * (x)

The above statement defines a macro named as SQUARE, which takes one parameter.

The parameter passed to is replaced with x.

Example: call to the above macro is given below:

SQUARE(2)

Replacement to be above macro:

SQUARE(2)   (2) * (2)

After multiplying 2 with 2 it will return the result as 4.

c)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

// main function definition
int main()
{
// Declares an integer array of size 10
int ivec[10];
// To store the n value entered by the user
int n;
// Loop variable
int c;
// Declares a pointer of type float
float *fvex;
// Accepts the value of n
printf("\n Enter the value of n: ");
scanf("%d", &n);
// Dynamically allocates memory to the pointer using malloc function
// Size is depending on the value of n entered by the user
fvex = (float *) malloc(n * sizeof(n));
// Assigns 2.7 to first index position
// 2.7f -> f is used after the constant because, f is a qualifier for floating constant
// Otherwise computer will treat the constant as double
fvex[0] = 2.7f;

// Loops from 1 to n, because 0 index position is already used
for(c = 1; c < n; c++)
// Calculates the cube of the previous index position of the array and stores it in current index position
fvex[c] = pow(fvex[c-1], 3);
// Loops till n
for(c = 0; c < n; c++)
// Displays each element of the array with two decimal places
// .2f -> .2 is used for two decimal places
printf("%.2f\t", fvex[c]);
}// End of main function

Sample Run:

Enter the value of n: 3
2.70 19.68 7625.60

Add a comment
Know the answer?
Add Answer to:
(a) Write a C program to print a list of all integers between 1 and 1,000...
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
  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • P 6.1 page 373, Write a program to initialize a list with 10 random integers from...

    P 6.1 page 373, Write a program to initialize a list with 10 random integers from 0 to 1000(inclusive) and output the following: (10 points) import random SEED = int(input("Input seed: ")) random.seed(SEED) print("\na. Every element at an even index") print("\nb. Every even element") print("\nc. All elements in reverse order.") print("\nd. Only the first and the last element.")

  • *C CODE* 1. Declare an array of doubles of size 75 called scoreBonus and initialize all...

    *C CODE* 1. Declare an array of doubles of size 75 called scoreBonus and initialize all the elements in the array to 10.5 2. Declare an array of integers called studentNumbers The array can hold 112 integers Prompt and get a number from the user for the third element in the array Prompt and get a number from the user for the last element in the array

  • Write a c program to implement threads. Accept an array of 40 integers, write a thread...

    Write a c program to implement threads. Accept an array of 40 integers, write a thread method named sum of array elements. Divide the array into 4 equal elements and call threads to find the sum of each part. Store the sum in a variable called “arrays'. Print the sum in the main function. (10 points)

  • Please complete in C++ (histogram.cpp) Write a pseudocode and C++ program that reads numbers from an...

    Please complete in C++ (histogram.cpp) Write a pseudocode and C++ program that reads numbers from an array (use another function to initialize the array with random values between 1 and 20 inclusive) and graphs the information in the form of a bar chart or histogram-each number is printed, then a bar consisting of that many asterisks is printed beside the number. Here is a sample run of the program: Element Value 12 Histogram ************ * **** * * * *...

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
ADVERTISEMENT