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
(a) Write a C program to print a list of all integers between 1 and 1,000...
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...
write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...
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...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
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...
c programming. Given a sorted array of 20 integers and a target integer value, your program needs to print out all pairs of integers which have sum equal to the target value. If there is no pair of integers to return, print out “No pair of integers in the given array can be summed to the target value”. The array of integers and the target value should be given from the keyboard (taken as user input). Inputs and Outputs should...
(+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 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 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.")
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)