answersLogoWhite

0

📱

Computer Programming

A category for questions about computer programming and programming languages.

10,506 Questions

Why c is top down approach?

It doesn't. Programmers might use any approach they like.

How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Please ask just one question at a time!
Question 1:

How do you declare an array of three pointers to chars?

How do you declare an array of three char pointers?


Note: both of these questions are merely alternative wordings for the same question.


Answer 1:

char * a[3];


Question 2:

How do you declare a pointer to an array of three chars?


Answer 2:

char a[3]; // an array of three chars

char * p = a; // a pointer to an array of three chars


Question 3:

How do you declare a pointer to a function which receives an int pointer?


Answer 3:

#include


// some functions we can point at:

void func_1(int * p){}

void func_2(int * p){}

// note: all functions we wish to point at with the same

// pointer must have the same signature.


int main()

{

int* p = NULL; // instantiate an int pointer

void (*pFunc) (int*); // declare a function pointer

pFunc = func_1; // point to func_1

pFunc(p); // call func_1 via function pointer

pFunc = func_2; // point to func_2

pFunc(p); // call func_2 via function pointer

return(0);

}


Note that the brackets in the function pointer declaration are required. If you omit them, you will end up with a standard function declaration that returns a pointer to void, resulting in a compiler error.





What is the difference between magnetic media and optical media?

Optical media is written and read on disks with lasers, such as a CD-Roms, BluRays, and DVDs. Magnetic media is written and read with magnets (like your hard drive and a floppy disks (which are almost non-existant anymore)).

What does an abstract look like?

nothing its a piece of art that's nothing really. you remember when you were little when you scribble scrabbled, that was abstract!

What are some types of bugs in software?

Examples of software bugs.

1. Off by one. For instance, If you use an 8-bit register or area of memory, you can only have a range of 0 to 255. However, suppose your code is off by one in an operation. One possible outcome is that the program will crash with an overflow error.

2. Using the wrong Boolean or numeric operator. You might use "less than" when you mean "less than or equal." Or you may interchange AND and OR. So the logic does not work as expected due to using the wrong word or symbol. You might be trying to trap errors and instead, you cause errors.

3. Access after free. If you release a block of memory a program is using, you are not supposed to keep using it. Some other code could be using it. If you read after releasing the block, the data cannot be trusted, since something might have modified it. If you write after releasing the block, you could be corrupting what is used elsewhere.

4. Buffer overrun. That is when a memory operation writes more data to a memory location than declared, and puts data past just the location you are modifying. So it ends up corrupting other data or even the program itself, another program, or the operating system. That will certainly lock up the computer with a blue screen.

Why wont this python program print work?

This is the program that I couldn't fit in the question: Sorry for the underscores but it wouldn't let me indent

# !/usr/bin/python

# Filename: prime5.py

#<---program is supposed to print prime number chosen--->

y = int(input('How many prime numbers do you want to see?'))

x = 6

w = 0

primes = [2,3,5]

z = len(primes)

j = 0

while w != 1:

____if y*0!=0:

________y = input('Please pick another NUMBER: ')

____elif y*0==0:

________w = w + 1

while z <= y:

____if x % primes[j] (j+1) and x % primes[j] != 0:

________primes.append(x)

________x = x + 1

________j = 0

____elif x % primes[j] != 0:

________j = j + 1

print ('These are the numbers you wanted to know', primes)

Difference between event driven programming and traditional programming?

In traditional programming, code was run from start to finish and sometimes asked for input along the way. Event driven programming, on the other hand, does nothing until it gets an event, such as a mouse moving or a key being pressed.

Difference between machine language and binary code?

Machine language is often (incorrectly) referred to as binary language. Binary is a numbering system comprised of zeros and ones. (Also thought of as true/false, or originally from the electronic ancestry, on and off.)

Machine language is the instructions that computers understand. These instructions are comprized of binary values which instruct the CPU to perform specific actions.

When is source code freely available for a program?

In one of two conditions:

  1. public domain code - anyone can do anything they want with this source
  2. open source code - this source is legally copyrighted, but distributed with a licence controlling how you can modify and redistribute

What are the advantage of function prototype?

It allows the compiler to verify that the number and types of the functions parameters are correct when the function is actually called.

If the function were called with the wrong number or type of parameters, and no function prototype were supplied, the error would not be discovered until the program was actually run.

What is the Decimal Binary and Hexadecimal system?

It is a numerical system where each significant numeral represents a change of 2^16th power. Decimal, or, "base 10", is the normal system of decimals. For example, 124 is "10 ^ 2 + 2 * 10 ^ 1 + 4 * 10 ^ 0" (or "one hundred twenty four"). In hexadecimal, each position is 16 base units instead of 10. This makes reading binary code easier, as binary and hex easily convert to each other directly.

Is c sharp a case sensitive language?

Yes.

The upper/lower case chars are regarded as being different in C.

For example, you can have two variables, one called 'x' and the other called 'X' and the compiler will recognize them as separate variables.

What does a high CO2 level indicate?

It means that the levels of carbon dioxide in the blood are increased usually causing a deficient level of oxygen. This condition can cause cells to die.

A device driver is a computer program that makes communication possible between your computer and one of its peripherals?

a device driver is software, making communication between your motherboard and your OS, probably Windows, possible.

Program to check whether a number is a palindrome or not using functions?

There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.

A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.

The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.

The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.

The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.

The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.

You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.

#include<iostream>

#include<string>

#include<vector>

using namespace std;

string input_string(string prompt)

{

cout<<prompt<<":\t";

string input;

getline(cin, input, '\n');

return(input);

}

void convert_tolower(string& s)

{

for(string::iterator i=s.begin(); i!=s.end(); ++i)

*i=tolower(*i);

}

vector<int> strip(const string& s)

{

vector<int> v;

v.push_back(-1);

for(int i=0; i<s.size(); ++i)

{

if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))

{

v.push_back(i);

v.push_back(-1);

}

}

return(v);

}

bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)

{

for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)

if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))

break;

++x, --y;

if( v[x]==-1 )

++x, --y;

return(x>=0 && x<y && y-x>1);

}

int main()

{

string input;

while(1)

{

input=input_string("Enter a string");

if(input.size()==0)

break;

string copy(input);

convert_tolower(copy);

vector<int> v=strip(copy);

string pal;

int pos=0;

for(int i=0; i<v.size(); ++i)

{

int start=0, end=0;

if( ispalindrome( copy, v, i, start, end))

{

string tmp( input.substr(v[start],v[end]-v[start]+1));

if( tmp.size() > pal.size() )

{

pal = tmp;

pos = v[start];

}

}

}

if( pal.size() )

{

cout<<"Palindrome:\t";

for(int i=0; i<pos; ++i)

cout<<" ";

cout<<pal<<"\n"<<endl;

}

else

cout<<"The string contains no palindromes!\n"<<endl;

}

return(0);

}

Example output:

Enter a string: Madam, I'm Adam

Palindrome: Madam, I'm Adam

Enter a string: Madam, I'm Adam is a palindrome

Palindrome: Madam, I'm Adam

Enter a string: In girum imus nocte et consumimur igni

Palindrome: In girum imus nocte et consumimur igni

Enter a string: 0123456765432

Palindrome: 23456765432

Enter a string:

Press any key to continue . . .

What is this the binary code of 01000010 01100001 01100011 01101011 00100000 01110101 01110000 00100000 01101101?

This depends upon what "base" you are referring. In "base 10", 111 is 1101111.
In "base 8", 111 is 1001001
In "base 16", 111 is 100010001.
Of course, if you are just asking "what is the value of 111 in base 10" that would be 7. (4 + 2 + 1)

How do you write a code that displays the dimension of a letter size 8.5 x 11-inch sheet of paper in millimeters if there are 25.4 millimeters per inch?

To get you started, you need a simple conversion function to convert inches to millimeters. Here's an example using the literal constant 25.4:

constexpr double convert_inch_to_mm (const double inches) {return inches * 25.4;}

What is the language processor?

Language processors are language translation software like assembler, interpreter and compiler

Why is it important to have a solid background in mathematics for programming languages?

You need to to know some basic math to live. right? Likewise you need to know some basic math to start computer programming....beginners start out by learning to do programs like solving a quadratic equation....if you are not thorough with algebra, u can't do it. The way you think is more important.....rather than being good in math, computer programmers are required to think the way math people think - vey very very logically.

A multithreaded program that generates the Fibonacci series using Pthreads?

#include<stdio.h>

#include<time.h>

#include<pthread.h>

#include<stdlib.h>

#include<sys/types.h> /* need to calculate which I will implement later */

void *fibr(void *n);

void *fibr_1(void *k);

signed long long int fibonacci(signed long long int);

int main(){

clock_t begin, end;

double time_spent;

pthread_t tid,tid1;

int result,result1;

signed long long int n=6;

signed long long int m=7;

result=pthread_create(&tid,NULL,fibr,&n);

if(result){

perror("pthread_create");

return 1;

}

result1=pthread_create(&tid1,NULL,fibr,&m);

if(result1){

perror("pthread_create");

return 1;

}

if(pthread_join(tid,NULL)){

perror("pthread_join");

return 1;

}

if(pthread_join(tid1,NULL)){

perror("pthread_join");

return 1;

}

printf("Fib value=%lld\n",n+m);

pthread_exit(NULL);

}

void *fibr(void *n){

signed long long int *y=n;

signed long long int x=*y;

pthread_t tid2,tid3;

signed long long int i,j;

/* How do I assign values to i , j in order to

achieve the level viz fib(n-2)....fib(n-4) */

if(pthread_create(&tid2,NULL,fibr_1,&i))

{

perror("pthread_create");

}

if(pthread_create(&tid3,NULL,fibr_1,&j))

{

perror("pthread_create");

}

if(pthread_join(tid2,NULL))

{

perror("pthread_join");

}

if(pthread_join(tid3,NULL))

{

perror("pthread_join");

}

/* How to return the values of i, j combined with *y . if I do *y+i+j, the result

is not coming correctly */

*y=fibonacci(x);

return NULL;

}

void *fibr_1(void *k){

long long int *a=k;

long long int b=*a;

*a=fibonacci(b);

return NULL;

}

signed long long int fibonacci(signed long long int x){

if((x==0)(x==1))

return x;

return fibonacci(x-1)+fibonacci(x-2);

}

What is a bit in a computer code?

A bit is the smallest unit of storage in a computer. It represents either a 0 or a 1, which comes from the electrical basis of a computer in which current can either be flowing (1) or not flowing (0).

What are the applications of this pointer?

The this pointer is used to refer to the object that is foremost in the current scope. It has three primary purposes. First, a programmer uses this to refer to a member variable that has been shadowed by a function argument. Secondly, it is used to obtain a reference to itself, usually for the purpose of passing itself to another function or class. Finally, the this pointer can be used to call alternate constructors of the same object, usually as a means of reusing code and providing various constructors with default arguments.