Question

In: Computer Science

Write a c statement to declare variables of primitive data types, and assign values to the...

  1. Write a c statement to declare variables of primitive data types, and assign values to the variables.
  2. Use common control structure in c, such as if, switch, break, for, while statements.
  3. Write a c program that takes command line parameters, and perform some computation based on the command line input.
  4. Write c statements to generate random number within a certain range.
  5. Write a c statement to declare array variables of primitive data types, and assign values to the elements of array type variables.
  6. Write a c statement to loop through an array to find a certain element, or the sum, maximum, minimum values of all array elements.
  7. Read user input using scanf() function, and output contents of variables using printf() function.
  8. Declare pointer type of variables, and set a pointer to the address of a variable, or certain element of an array.
  9. Declare an array assign values to the entries in the array, display the values stored in the array.
  10. Program using 2d arrays.

Solutions

Expert Solution

c statement to declare variables of primitive data types, and assign values to the variables

ans:

#include <stdio.h>

int main(){

int a=10;

float b=1.5;

char c='a';

char d[5]="hello";

double e=1.9393;

printf("a=%d\n",a);

printf("b=%f\n",b);

printf("c=%c\n",c);

printf("d=%s\n",d);

printf("e=%lf\n",e);

return 0;

}

Use common control structure in c, such as if, switch, break, for, while statements

ans:

#include <stdio.h>

int main(){

int i=0;

while(i<10){

if(i%2==0){

printf("even");

}else{

printf("odd");

}i++;

}

char a='d';

for(int i=0;i<10;i++)printf("%d ",i);

switch(a){

case 'a':

printf("\na is printed\n");break;

case 'd':

printf("\nd is printed\n");break;

default:

printf("\ndefault one is printed\n");

}

}

Write a c program that takes command line parameters, and perform some computation based on the command line input.

ans: which is product of 2 numbers

#include <stdio.h>

#include <stdlib.h>

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

int a=atoi(argv[1]);

int b=atoi(argv[2]);

printf("%d x %d = %d\n",a,b,a*b);

return 0;

}

Write c statements to generate random number within a certain range

ans:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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

int max_num=56;

int min_num=12;

srand(time(0));

int random_num_range = (rand() % (max_num - min_num+ 1)) + min_num;

printf("%d\n",random_num_range);

return 0;

}

Write a c statement to declare array variables of primitive data types, and assign values to the elements of array type variables.

ans:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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

int a[4]={1,2,5,4};

double b[3]={1.2,4.5,8.9};

char c[3]={'a','b','d'};

float d[4]={3.4,2.4,5.6,1.2};

return 0;

}

Write a c statement to loop through an array to find a certain element, or the sum, maximum, minimum values of all array elements.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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

int a[6]={4,6,9,2,3,8};

int min_index=0,max_index=0,sum=0;

for(int i=0;i<6;i++){

if(a[i]<a[min_index]){

min_index=i;

}

if(a[i]>a[max_index]){

max_index=i;

}

sum=sum+a[i];

}

printf("minimum element: %d\n",a[min_index]);

printf("maximum element: %d\n",a[max_index]);

printf("sum of element: %d\n",sum);

}

Read user input using scanf() function, and output contents of variables using printf() function.

ans:

#include <stdio.h>

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

printf("Enter number:");

int n;

scanf("%d",&n);

printf("You enter number is %d\n",n);

return 0;

}

Declare pointer type of variables, and set a pointer to the address of a variable, or certain element of an array.

ans:

#include <stdio.h>

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

int a[4]={1,2,5,8};

int *ptr;

ptr=a;

for(int i=0;i<4;i++){

printf("%d\n",ptr[i]);

}

}

Declare an array assign values to the entries in the array, display the values stored in the array.

Program using 2d arrays.

ans:

#include <stdio.h>

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

int a[2][3]={{1,4,6},{6,8,9}};

for(int i=0;i<2;i++){

for(int j=0;j<3;j++){

printf("%d ",a[i][j]);

}printf("\n");

}

}


Related Solutions

write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables...
write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables and display the resulting value.
Question 1 Please create three different types of variables and assign three different values into the...
Question 1 Please create three different types of variables and assign three different values into the variables. Please create a new variable and assign the sum of two variables above. Please display all the data (four data here) as a line. Question 2 using Scanner, take three inputs from a user (job level, the number of years in company, and job title) and display yearly bonus percentage based in the following rule: job level : 1 to 10 Job title...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
For the following types of values, designate discrete variables (D) and continuous variables (C): (a) weight...
For the following types of values, designate discrete variables (D) and continuous variables (C): (a) weight of the contents of a package of cereal, (b) diameter of a bearing, (c) number of defective items produced, (d) number of individuals in a geographic area who are collecting unemployment benefits, (e) the average number of prospective customers contacted per sales representative during the past month, (f) dollar amount of sales.
Create your own data type using a C++ structure. Assign values to a variable of that...
Create your own data type using a C++ structure. Assign values to a variable of that data type. Retrieve values from the variable of that data type. Description: In this lab you will choose the statements to create a program that: Creates a brand new data type called "Monster". Once created, creates a variable of type Monster and assign values to the variable and then display the contents. This lab will again take the form of a guided multiple-choice quiz...
In Java form Declare and assign a char variable called myLetter to z. Write a Java...
In Java form Declare and assign a char variable called myLetter to z. Write a Java statement that makes myLetter uppercase. Write a java statement that finds out if myLetter is a digit. Write a java statement that finds out if myLetter is an upper case letter. Assume myLetter = ‘Z’; What is the output for System.out.print(myLetter++); Assume myLetter = ‘Z’; What is the output for System.out.print(++myLetter); Assume myLetter = ‘Z’; What is the output for System.out.print(myLetter--); What happens when...
Python HW with Jupyter Notebook Declare a variable named DATA as a dictionary object. Assign the...
Python HW with Jupyter Notebook Declare a variable named DATA as a dictionary object. Assign the set of key/value pairs shown below. Create a function named iter_dict_funky_sum() that takes one dictionary argument.    Declare a running total integer variable. Extract the key/value pairs from DATA simultaneously in a loop. Do this with just one for loop and no additional forms of looping. Assign and append the product of the value minus the key to the running total variable. Return the funky...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
in C++ You will overload a function named printArray. The function will assign values to each...
in C++ You will overload a function named printArray. The function will assign values to each element and print out a single dimension array of integers and an array of characters. The main body of the program will call each function. (6 points) Using the player switch portion of the tic tac toe game, create a function that does the switch. This is a practice in applying local variables, parameters, and global variables. (4 points)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT