Question

Coding

Question :

You are given a file results.txt with the results of an experiment as a set of integers. You need to separate these results into three categories: negative integers, even positive integers, and odd positive integers.

 

A sample of the file results.txt is shown below:

    52    49   -22    31   -66    41   -94   -45   -91   -81    65    39   -37    90   -94   -12   -24    53    59   -63    -2

   -11    29    42    51   -45    36    31   -68   -77     0    92   -32    17   -56    51   -49     1    40    79    92     9

   -73   -70   -49    68   -49    63   -52    86   -30   -61   -50    23    -5   -30    66    17    10    84   -43    52    51

Write a complete C++ program that reads the data from the file results.txt and separate it into the following files:

1.     A file negative.txt that contains the negative integers from the results.

2.     A file even.txt that contains the even positive integers from the results.

3.     A file odd.txt that contains the odd positive integers from the results.

 

Notes: The number of experiments is not greater than 1000, but the exact number is not known beforehand. Your program needs to check whether the file results.txt exists or not.

 

 

The negative.txt file might contain data similar to the following (note that the numbers are separated with a space):

-22 -66 -94 -45 -91 -81 -37 …

The even.txt file might contain data similar to the following (note that the numbers are separated with a space):

52 90 42 35 0 92 40 …

The odd.txt file might contain data similar to the following (note that the numbers are separated with a space):

49 31 41 65 39 53 59 …


 

 

Question:

1.     Write a program to create a new type called complex using structures. This new type will have only two double numbers: real and imag to represent the real and imaginary parts of a complex number.                                           

2.     Declare two complex numbers X and Y in the function main and ask the user to enter the real and imaginary parts of these numbers using the keyboard.                

3.  Define a function called display that accepts a complex number as a parameter and will display its real part and its imaginary part. This function returns nothing.       

4.     Use the function display in the function main to display the real and imaginary parts of X and Y.                                             

5.     Define a function called add that accepts two complex numbers as parameters and it will return the complex number that is equal to their sum. To add two complex numbers, just add the corresponding real and imaginary parts.                           

6.     Declare a complex number Z in the main function and assign the call to the function add to Z using the parameters X and Y. Use the function display in the function main to display the real and imaginary parts of Z.                                 

Sample execution (user inputs are in bold):

Please enter the first complex number X 10 15

Please enter the second complex number y 35 22

(10,15)

(35,22)

(45,37)

 



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

#include<stdio.h>

struct complex{ //Definging a complex number

  double real;

  double imag;

};

typedef struct complex Complex;

void display(Complex x); //Prototypes

Complex add(Complex x, Complex y);

int main(){

  Complex X, Y, Z; //Declaring two complex numbers

  printf("Enter the first complex number: ");

  scanf(" %lf %lf", &(X.real), &(X.imag));

  printf("Enter the second complex number: ");

  scanf(" %lf %lf", &(Y.real), &(Y.imag));

  display(X); //Displaying complex numbers

  printf(" ");

  display(Y);

  printf(" ");

  Z = add(X, Y);

  display(Z);

  return 0;

}

//Function definition

void display(Complex x){

  printf("(%.1lf,%.1lf)", x.real, x.imag);

}

Complex add(Complex x, Complex y){

  Complex z;

  z.real = x.real + y.real;

  z.imag = x.imag + y.imag;

  return z;

}


answered by: Allen

> Thank you

Meitantei Wed, Nov 24, 2021 8:03 PM

Add a comment
Know the answer?
Add Answer to:
Coding
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++

    You are given a file results.txt with the results of an experiment as a set of integers. You need to separate these results into three categories: negative integers, even positive integers, and odd positive integers. A sample of the file results.txt is shown below:    52    49   -22    31   -66    41   -94   -45   -91   -81    65    39   -37    90   -94   -12   -24    53    59   -63    -2   -11    29    42    51   -45    36    31   -68   -77     0    92   -32    17   -56    51  ...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...

  • need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re,...

    need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re, im; // the real and imaginary parts of a complex number }; typedef struct _cplx Complex; // Initializes a complex number from two real numbers Complex CmplxInit(double re, double im) Complex z = { re, im }; return z; // Prints a complex number to the screen void CmplxPrint(Complex z) // Not printing a newline allows this to be printed in the middle of...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • Consider the following C struct that represents a complex number. struct complex {    double real;    double...

    Consider the following C struct that represents a complex number. struct complex {    double real;    double imaginary; }; (a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class: A default constructor that initializes the real and imaginary parts to 0. A constructor that allows initialization of both real and imaginary parts to any double value. A public member function that returns the magnitude of the complex number....

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

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