Question

Part 1: Determine what is wrong (if any) with the following statements int A[4] = {...

Part 1:

Determine what is wrong (if any) with the following statements

  1. int A[4] = { 1, 2, 3, 4, 5};
  2. int A[4] = {1,2};
  3. int A[4] = [1,2,3,4];

Part 2:

Create one dimensional array and save the following integers in it:  2, 3, 6, 7, 10, 12. Display the values on the screen. (for example, a[0] = 2, a[1] = 3, etc.)

Part 3:

Write a program to read 5 integers from the user. Save these integers in an array and display the following on screen:

  1. The entered values
  2. The average value

Part 4:

Create a function that will do the following (when called):

Generate 5 random numbers between 1 – 10. (Hint: use rand() function)

Save the random numbers in an array (Hint: create an array)

Display these numbers on the screen by reading them from the saved array.

Homework Answers

Answer #1

Part 1:

  1. int A[4]={1,2,3,4,5}; -> This will give us error because the array is declared with size 4 but five elements are given. So the array cannot initialize the number 5.
  2. int A[4] = {1,2}; -> This will not cause any error. Though the size is declared 4, the remaninig elements will be 0. The array will be {1,2,0,0}
  3. int A[4] = [1,2,3,4]; -> The initialization is wrong here. This is not the correct syntax for initialization. Use curly braces {} instead of brackets.

Part 2:

#include <stdio.h>

int main()
{
   int a[6]={2,3,6,7,10,12};  //single dimension array declared and initialized with the given values
    printf("The values of array a[] are: ");
    for(int i=0;i<6;i++)
    {
       printf("%d ",a[i]);//printing the array values
    }

    return 0;
}

Output: The values of array a[] are: 2 3 6 7 10 12

Part 3:

#include <stdio.h>

int main()
{
    int a[5],i,sum=0;  //single dimension array declared with size 5
    double average;
    printf("Input the 5 values one by one : \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a[i]); //Input the array values
    }
    printf("Entered 5 values are : \n");
    for(i=0;i<5;i++)
    {
        printf("%d ",a[i]); //Input the array values
    }
    printf("\n"); //new line
    for(i=0;i<5;i++)
    {
        sum=sum+a[i]; //add all the values in array to sum
    }
    average=sum/5; //calculate average
    printf("The average of all the 5 values is: %.2f ",average);
    return 0;
}

Output: The ouput for the above code is,

Input the 5 values one by one :
1
2
3
4
5
Entered 5 values are :
1 2 3 4 5
The average of all the 5 values is: 3.00

Part 4:

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
void generate()
{
    int a[5],i;
    printf("Generating 5 random values between 1 and 10\n");
    for(i=0;i<5;i++)
    {
        a[i]=rand()%10+1; //here we generate random values between 1 and 10 using rand() function and store them in array
    } 
    printf("Values generated and stored in array\n");
    printf("Displaying the random generated values: ");
    for(i=0;i<5;i++)
    {
        printf("%d ",a[i]); //display the random values
    }
}
int main()
{   srand(time(0));  //this will give the rand() a new seed so that you will get newly generated random values every time. If you dont use this you will get the same values every time you execute the program.
    generate(); //call the generate() function
    return 0;
}

Output:

Generating 5 random values between 1 and 10
Values generated and stored in array
Displaying the random generated values: 4 7 8 6 4

#Please dont forget to upvote if you find the solution helpful. Thank you.

Know the answer?
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
Similar Questions
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create...
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create a 2-by-3 integer array and initialize it with 6 integers of your choice. Using nested for loops, display the contents of the array on the screen Part 2: (Continue on part 1) Display the sum of all the integers in the array that you created in part 1. Create a function Part 3: Create a function that will display the elements of the array...
1- Create a new script that is called HW9.m and save it. The script will do...
1- Create a new script that is called HW9.m and save it. The script will do the following. Test your code for each of the cases and upload your script as a .m file. [15 points] a. Create a random integer numbers that goes from 0 to 5 and assign it to a variable y using rand and round commands. Hint: Note that rand function only creates random number between 0 and 1. You need to scale the values to...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into...
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into the function – the distribution from which to generate random numbers. b.There are two arguments to put into the function – the number of random values to generate, followed by the number of decimal places. c.The RAND function has no arguments, just parentheses with nothing in between. 2. What do you actually type into a spreadsheet cell to use the RAND function? a. =RAND()...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
Your assignment is to write a c++ function that can calculate values for any 5 th...
Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5 Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x). The second parameter should be an array with 6 elements that contain the coefficients a0 to a5. The output of your function should be the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT