Question

1. (40 pts) Note: All programs MUST run in Ubuntu. Following the example C program in...

1. (40 pts) Note: All programs MUST run in Ubuntu. Following the example C program in Folio consisting of three files (a.c, a.h, main.c), write a C program that consists of three files mysquare.h, mysquare.c andmyMain.c. Below is the mysquare.h prototype

#include <stdlib.h>

#include <stdio.h>

void generateNums(int *myarr, int len);

void squareNums(int *myarr, int len);

void printNums(int *myarr, int len);

  • mysquare.h must contain only the function declarations (prototypes) listed above
  • generateNums function should generate len random integers in the range 0~99 inclusive (use randfunction in <stdlib.h>) in the supplied array myarr. Its definition should be in mysquare.c.
  • squareNums function should square the len integers stored at myarr. Its definition should be in mysquare.c.
  • printNums function should print the len integers stored at myarr on one line with suitable separators between them. Its definition should be in mysquare.c.
  • Note that the caller of the above three functions should pass valid pointers myarr to them.
  1. In myMain.c you need to write your main function that calls the three functions in mysquare.h to generate N random integers in the range 0~99 inclusive and then print out the generated random numbers on one line and the squared N numbers on another line. Your program should get N(which represents the number of random integers you will generate) from the first command line argument (i.e. argv[1]) of this C program (you can use the atoi function; see example code LeapYear_cmdline.c). You need to decide where to store the N generated integers and where to store the N squared integers. Submit all the three code files. (37 pts)
  2. Submit a makefile to compile your program. Be aware of the possible file name issue regarding Folio complaint. See the last slide of 3_Pointers_Functions.pptx for details. Make sure you have tested that your makefile works. (3 pts)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here are the code for the 3 files. You need to create 3 files with extensions - .h, .c and .c and copy paste the below code in the respective files.

Let me know for any help with any other questions.

====================================================================

//mysquare.h file

# include <stdlib.h>

# include <stdio.h>

void generateNums(int *myarr, int len);

void squareNums(int *myarr, int len);

void printNums(int *myarr, int len);

====================================================================

//mysquare.c file

# include <stdlib.h>

# include <stdio.h>

void generateNums(int *myarr, int len){

               

                int i =0;

               

                for(i= 0; i<len; i++){

                               

                                *(myarr+i) = rand()%100;

                }

}

void squareNums(int *myarr, int len){

               

                int i;

                for(i=0; i<len; i++){

                               

                                *(myarr+i) = (*(myarr+i))*(*(myarr+i));

                }

}

void printNums(int *myarr, int len){

               

                int i=0;

                for(i =0; i<len; i++){

                                printf("%d ",*(myarr+i));

                }

                printf("\n");

}

====================================================================

// main.c file

# include "mysquare.h"

int main(int argc, char** argv) {

                               

                                int N = atoi(argv[1]);

                               

                                int *arr = (int*) malloc(N*sizeof(int));

                               

                                generateNums(arr,N);

                                printNums(arr,N);

                                squareNums(arr,N);

                                printNums(arr,N);

                               

                                free(arr);

        return 0;

}

====================================================================

Add a comment
Know the answer?
Add Answer to:
1. (40 pts) Note: All programs MUST run in Ubuntu. Following the example C program in...
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
  • Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...

    Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need to understand function, pointer and the selection sort algorithm. Write a C program that consists of three files mysort.h, mysort.c and myMain.c. Below is the mysort.h prototype #include <stdlib.h> #include <stdio.h> void generateNums(int *myarr, int len); void sortNums(int *myarr, int len); mysort.h must contain only the function declarations (prototypes) listed above generateNums function should generate len random integers in the...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • C++ this program will get two integers from the user. The program will 1. call getProduct...

    C++ this program will get two integers from the user. The program will 1. call getProduct (int, int, int &); where the first two arguments are the two input integers and third one is the product of these 2 integers. 2. call printEvenAscending (int, int); where the two arguments are the two input integers. This function will print all -even numbers between the two arguments in an ascending order. Note, the two input integers could be in any order (such...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of...

    **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of your own design. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called playthat should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var...

  • This program will use the Random class to generate random temperatures (integers) between some minimum (inclusive)...

    This program will use the Random class to generate random temperatures (integers) between some minimum (inclusive) and some maximum (inclusive) values. You will need to calculate the minimum and maximum given a starting temperature integer and a possible change in temperature integer. (1) Copy the following method stub into your Temperature Predictor class and complete it according to the specifications described in the header comments. * Generates a random temperature (int) within a range when given the current temperature *...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60...

    Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the update separately as...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

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