I've been ask to choose 4 different types of fundamental data types in C++ then write C++ code statements to declare and initialise a variable of each type...I really dont understand what this question is talking about but 4 data types I could think of are:
Integers, Float, Characters and Double
Could someone clarify what the question means and please a little help on it?????????? Thanks guys and gals =).........
C++ code statements....?
declaring is like saying there is an identifier called with "...."
example:
int i; (we delared an integer i)
initializing the variable
i = 0; (initial value of i is 0)
declaring and initializing at the same time is like:
int i = 0;
the 4 data types are as follows:
int (for integers)
float (for numbers with decimal points)
char (for characters)
double (for higher precision numbers)
Reply:Sounds like you understand it! Just write 4 statements that declare and inititalize each of those types!
Reply:It's pretty basic stuff.... Decare a variable of each type, e.g.
int num;
float cost;
bool choice;
// etc etc
Then initialize them by assigning each a value...
num=1;
float=1.0
bool = true;
Alternatively, you could just declare and initialize together,
int num=1;
float cost=1.0;
bool choice=true;
Reply:Sorry sis, mi no save so mi nonap ansarim displa
Reply:struct myvariablen{
int ia;
float fa;
char ca;
double da;
};
then in programm u can use it like this:
myvariablen myvar;
myvar.fa = 3.2;
myvar.ca = "a";
myvar.ia="2";
hamper
Wednesday, July 8, 2009
Can you use C code in a C++ file?
The enviornment/makefile is setup to build both C and C++. Just wondering if this is common to use C code in a C++ file or if it is not something someone should do.
Thanks!
Can you use C code in a C++ file?
C++ is C language + object programming.
So when you are programming in C++, you are using an object which call (in an other file) some premade C language.
So yeah you can and withouth knowing you are doing it...
int i=1 is C code... can you program in C++ without using C?? No!
Reply:Yes, it is. C is a subset of C++, so most things valid in ANSI C work fine in C++ (if you are using an ANSI compiler; most are). I often use C when using C++, I mainly use C++ for classes, I do everything else in standard C library.
Reply:Yeah but it's the library that differs on most compilers.
Thanks!
Can you use C code in a C++ file?
C++ is C language + object programming.
So when you are programming in C++, you are using an object which call (in an other file) some premade C language.
So yeah you can and withouth knowing you are doing it...
int i=1 is C code... can you program in C++ without using C?? No!
Reply:Yes, it is. C is a subset of C++, so most things valid in ANSI C work fine in C++ (if you are using an ANSI compiler; most are). I often use C when using C++, I mainly use C++ for classes, I do everything else in standard C library.
Reply:Yeah but it's the library that differs on most compilers.
C++ code about union and intersection operation ?
i am looking for a c++ code that can do the union , intersection and etc operation.
anyone has this code ???
C++ code about union and intersection operation ?
Here's my sample program:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
#include %26lt;algorithm%26gt;
using namespace std;
void nPrint(vector%26lt;int%26gt;);
vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
int main(int argn, char* argv) {
vector%26lt;int%26gt; f = vector%26lt;int%26gt;();
vector%26lt;int%26gt; s = vector%26lt;int%26gt;();
f.push_back(1);
f.push_back(2);
f.push_back(3);
f.push_back(4);
s.push_back(3);
s.push_back(4);
s.push_back(5);
s.push_back(6);
nPrint(nIntersection(f, s));
nPrint(nUnion(f, s));
nPrint(nDifference(f, s));
return 0;
}
void nPrint(vector%26lt;int%26gt; v) {
for (int i = 0; i %26lt; v.size(); ++i)
cout %26lt;%26lt; v[i] %26lt;%26lt; " ";
cout %26lt;%26lt; endl;
}
vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; v = (f.size() %26gt; s.size() ? f : s);
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; v.size(); ++i)
if (count(f.begin(), f.end(), v[i]) != 0 %26amp;%26amp; count(s.begin(), s.end(), v[i]) != 0)
res.push_back(v[i]);
return res;
}
vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; f.size(); ++i)
if (count(res.begin(), res.end(), f[i]) == 0)
res.push_back(f[i]);
for (int j = 0; j %26lt; s.size(); ++j)
if (count(res.begin(), res.end(), s[j]) == 0)
res.push_back(s[j]);
return res;
}
vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; f.size(); ++i)
if (count(s.begin(), s.end(), f[i]) == 0)
res.push_back(f[i]);
return res;
}
I really hope it helps you.
Sincerely,
Nikaustr
anyone has this code ???
C++ code about union and intersection operation ?
Here's my sample program:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
#include %26lt;algorithm%26gt;
using namespace std;
void nPrint(vector%26lt;int%26gt;);
vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt;, vector%26lt;int%26gt;);
int main(int argn, char* argv) {
vector%26lt;int%26gt; f = vector%26lt;int%26gt;();
vector%26lt;int%26gt; s = vector%26lt;int%26gt;();
f.push_back(1);
f.push_back(2);
f.push_back(3);
f.push_back(4);
s.push_back(3);
s.push_back(4);
s.push_back(5);
s.push_back(6);
nPrint(nIntersection(f, s));
nPrint(nUnion(f, s));
nPrint(nDifference(f, s));
return 0;
}
void nPrint(vector%26lt;int%26gt; v) {
for (int i = 0; i %26lt; v.size(); ++i)
cout %26lt;%26lt; v[i] %26lt;%26lt; " ";
cout %26lt;%26lt; endl;
}
vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; v = (f.size() %26gt; s.size() ? f : s);
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; v.size(); ++i)
if (count(f.begin(), f.end(), v[i]) != 0 %26amp;%26amp; count(s.begin(), s.end(), v[i]) != 0)
res.push_back(v[i]);
return res;
}
vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; f.size(); ++i)
if (count(res.begin(), res.end(), f[i]) == 0)
res.push_back(f[i]);
for (int j = 0; j %26lt; s.size(); ++j)
if (count(res.begin(), res.end(), s[j]) == 0)
res.push_back(s[j]);
return res;
}
vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {
vector%26lt;int%26gt; res = vector%26lt;int%26gt;();
for (int i = 0; i %26lt; f.size(); ++i)
if (count(s.begin(), s.end(), f[i]) == 0)
res.push_back(f[i]);
return res;
}
I really hope it helps you.
Sincerely,
Nikaustr
What is the c++ code runnable in dev c++ developmental tool, for this output????
what is the c++ code runnable in dev c++ developmental too, for this output????
it will accept a string and creates a figure,, like in the following:
(it will be like this if the number of amount of string is an even number)
enter string: ccss
c c
c
s
s s
(it will be like this if the number of amount of string is an odd number)
enter string: table
t t t
a a
b
l l
e e e
(note: it should be 3 strings to be accepted as input(string) in this program)
What is the c++ code runnable in dev c++ developmental tool, for this output????
what he heck is this? where did you find it
it will accept a string and creates a figure,, like in the following:
(it will be like this if the number of amount of string is an even number)
enter string: ccss
c c
c
s
s s
(it will be like this if the number of amount of string is an odd number)
enter string: table
t t t
a a
b
l l
e e e
(note: it should be 3 strings to be accepted as input(string) in this program)
What is the c++ code runnable in dev c++ developmental tool, for this output????
what he heck is this? where did you find it
Porting C code from 2.2 kernel to 2.6 RHEL kernel?
I have C code which is compiled in 2.2 kernel using Makefile. Now I am compiling the same code in 2.6 RHEL kernel using Make file. It throws an error "previous implicit declaration of 'function name' was here.
If I declare its prototype at the begining of the file I know it works. But I have many C files and many functions.
Is there any settings in Make file flag settings which will throw these errors as warnings
Porting C code from 2.2 kernel to 2.6 RHEL kernel?
Your issue has nothing to do with the kernel you are on. You would get the same issue on 2.2, 2.6, on Windows, Macs, etc.
%26gt; It throws an error "previous implicit declaration of 'function name' was here.
Either prototype your functions (declare them before using them), or move the definition above where you use it.
%26gt; If I declare its prototype at the begining of the file I know it works.
Sure. You should be declaring your functions before using them. So I'm guessing you don't do that?
%26gt; But I have many C files and many functions.
Is this code that you wrote? You may want to pick up a good book on C (google C FAQ. Somewhere there they have recommendations on books).
%26gt; Is there any settings in Make file flag settings which will throw these errors as warnings
No. And you don't seem to understand the purpose of errors/warnings. Compiling isn't a game of "try to get away with 0 errors". You want correct code. Errors means you made a syntactical error that *definitely* prevents your code from being compiled. You can't wish that away. Your code is broken. You can't compile broken code.
Warnings aren't something you ignore either. Why do you think we recommend compiling with -Wall? It's because warnings are important. Warnings are a diagnostic message. Your code compiles because it can be compiled. But that doesn't mean the code is correct. Always pay attention to warnings.
Fix your code. Learn C if you don't know how.
Reply:There are none, fix all the warnings.
bloom
If I declare its prototype at the begining of the file I know it works. But I have many C files and many functions.
Is there any settings in Make file flag settings which will throw these errors as warnings
Porting C code from 2.2 kernel to 2.6 RHEL kernel?
Your issue has nothing to do with the kernel you are on. You would get the same issue on 2.2, 2.6, on Windows, Macs, etc.
%26gt; It throws an error "previous implicit declaration of 'function name' was here.
Either prototype your functions (declare them before using them), or move the definition above where you use it.
%26gt; If I declare its prototype at the begining of the file I know it works.
Sure. You should be declaring your functions before using them. So I'm guessing you don't do that?
%26gt; But I have many C files and many functions.
Is this code that you wrote? You may want to pick up a good book on C (google C FAQ. Somewhere there they have recommendations on books).
%26gt; Is there any settings in Make file flag settings which will throw these errors as warnings
No. And you don't seem to understand the purpose of errors/warnings. Compiling isn't a game of "try to get away with 0 errors". You want correct code. Errors means you made a syntactical error that *definitely* prevents your code from being compiled. You can't wish that away. Your code is broken. You can't compile broken code.
Warnings aren't something you ignore either. Why do you think we recommend compiling with -Wall? It's because warnings are important. Warnings are a diagnostic message. Your code compiles because it can be compiled. But that doesn't mean the code is correct. Always pay attention to warnings.
Fix your code. Learn C if you don't know how.
Reply:There are none, fix all the warnings.
bloom
Help with C++ code.?
can any1 help me figure the code out to this question?
I'm stuck. Make it in c++.
The Question:
Write a function allValid that takes an array of integer grades and the size of the array as parameters. The function returns the index of the first value that is negative or over 100. If this is not true of any of the values, they are all valid, and the function returns the size of the array.
Write a driver to test this function. A driver in this context is code that controls other code. A test driver is a main() that calls the other code to test if it produces the correct result.
My code so far:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
void allValid(int, int);
int main()
{
int grades[] = {100, 90, 85, 76, 71};
const int DATA_SIZE = 5;
for(int x = 0; x %26lt; DATA_SIZE; x++)
}
void allValid(int grades[], int size[])
{
for(int x = 0; x %26lt; 0 %26amp;%26amp; x %26gt; 100; x = invalid)
if(x %26gt;= 0 || x %26lt;= 100);
}
Please try to help.
Thanks in advance.
Help with C++ code.?
You have a number of problems:
1. You never call your allValid subroutine in main().
2. You don't understand the for(;;) construct. Until you get some more experience with it, limit yourself the the vanilla :
for(%26lt;initialize the loop counter%26gt;; %26lt;condition necessary to do another pass%26gt;, %26lt;increment counter%26gt;)
3. A for statement executes the next statement after the for.
If you have multiple lines of code, you need to make a compound statement by enclosing those lines in braces.
If fact get in the habit of using braces every time you write a for, while or do-while loop. It makes it more obvious to code readers what is going on.
Braces are inexpensive. I usually buy them in bulk lots of 50kg.
4. You can't return a value if the return value of your subroutine is void. Actually you can, but that's beyond what you're attempting to do here.
5. Most programmers reserve i,j,k,l,m,n for loop counters. It's something that goes back to the days of FORTRAN
6. It's a good practice to start your loops at 0 and set the cut off at N where N is the size of your array. That way your counter will always be a valid index into the array.
Here is what I came up with:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
int allValid(int[], int);
int main()
{
int grades[] = {100, 90, 85, 76, 71};
const int DATA_SIZE = 5;
int returnValue = allValid(grades, DATA_SIZE);
return 0;
}
int allValid(int grades[], int size)
{
for(int i = 0; i %26lt; size; i++)
{
if(grades[i] %26lt; 0 || 100 %26lt; grades[i])
{
return grades[i];
}
}
//Fall through if all are OK
return size;
}
One other thing you can do:
const int DATA_SIZE = sizeof(grades)/sizeof(int);
That way if you add to the array of grades, you don't have to count them to get DATA_SIZE.
sizeof(grades) gives you the number of bytes in the array grades[], sizeof(int) gives you the number of bytes in int.
They are computed by the compiler so they can be used to set the value of a const variable.
I'm stuck. Make it in c++.
The Question:
Write a function allValid that takes an array of integer grades and the size of the array as parameters. The function returns the index of the first value that is negative or over 100. If this is not true of any of the values, they are all valid, and the function returns the size of the array.
Write a driver to test this function. A driver in this context is code that controls other code. A test driver is a main() that calls the other code to test if it produces the correct result.
My code so far:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
void allValid(int, int);
int main()
{
int grades[] = {100, 90, 85, 76, 71};
const int DATA_SIZE = 5;
for(int x = 0; x %26lt; DATA_SIZE; x++)
}
void allValid(int grades[], int size[])
{
for(int x = 0; x %26lt; 0 %26amp;%26amp; x %26gt; 100; x = invalid)
if(x %26gt;= 0 || x %26lt;= 100);
}
Please try to help.
Thanks in advance.
Help with C++ code.?
You have a number of problems:
1. You never call your allValid subroutine in main().
2. You don't understand the for(;;) construct. Until you get some more experience with it, limit yourself the the vanilla :
for(%26lt;initialize the loop counter%26gt;; %26lt;condition necessary to do another pass%26gt;, %26lt;increment counter%26gt;)
3. A for statement executes the next statement after the for.
If you have multiple lines of code, you need to make a compound statement by enclosing those lines in braces.
If fact get in the habit of using braces every time you write a for, while or do-while loop. It makes it more obvious to code readers what is going on.
Braces are inexpensive. I usually buy them in bulk lots of 50kg.
4. You can't return a value if the return value of your subroutine is void. Actually you can, but that's beyond what you're attempting to do here.
5. Most programmers reserve i,j,k,l,m,n for loop counters. It's something that goes back to the days of FORTRAN
6. It's a good practice to start your loops at 0 and set the cut off at N where N is the size of your array. That way your counter will always be a valid index into the array.
Here is what I came up with:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
int allValid(int[], int);
int main()
{
int grades[] = {100, 90, 85, 76, 71};
const int DATA_SIZE = 5;
int returnValue = allValid(grades, DATA_SIZE);
return 0;
}
int allValid(int grades[], int size)
{
for(int i = 0; i %26lt; size; i++)
{
if(grades[i] %26lt; 0 || 100 %26lt; grades[i])
{
return grades[i];
}
}
//Fall through if all are OK
return size;
}
One other thing you can do:
const int DATA_SIZE = sizeof(grades)/sizeof(int);
That way if you add to the array of grades, you don't have to count them to get DATA_SIZE.
sizeof(grades) gives you the number of bytes in the array grades[], sizeof(int) gives you the number of bytes in int.
They are computed by the compiler so they can be used to set the value of a const variable.
C code problem?
/* Sendind and receving values between functions */
#include%26lt;stdio.h%26gt;
void calsum();
void main()
{
int a,b,c, sum;
printf("\n Enter any three numbers");
scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);
sum= calsum(a,b,c);
printf("\nSum=%d,sum");
}
void calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
return (d)
}
This code is giving errors
So where I am wrong
C code problem?
Nithinpb is correct. However, this is one more thing. When you declare a function and you are writing out the actual code for the function, you need to give the passed values a data type. So it needs to be:
int calsum(int x, int y, int z)
Also, you need to declare "int d" outside the function, otherwise it will move out of scope once you leave the function and the program won't have a variable d (because you only declare the "int d" in the function, not in the main).
Reply:It should not be void calsum(x,y,z). You are not returning any value. In the main you are expecting a value. So, its giving error.
Make it, int calsum(x,y,z).
--
Nithin.
http://www.bluminut.com
Reply:You'll also get an error on the line with printf("\nSum=%d,sum"); It should be printf("\nSum=%d",sum); Your 2nd quotation is in the wrong place.
In the future, let us know what the error says and we can give better feedback.
Reply:void calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
return (d)
}
i think this line is the one giving errors because x, y, z are parameters and used without being declared, try this:
void calsum(int x,int y, int z)
{
int d;
d=x+y+z;
return (d)
}
#include%26lt;stdio.h%26gt;
void calsum();
void main()
{
int a,b,c, sum;
printf("\n Enter any three numbers");
scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);
sum= calsum(a,b,c);
printf("\nSum=%d,sum");
}
void calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
return (d)
}
This code is giving errors
So where I am wrong
C code problem?
Nithinpb is correct. However, this is one more thing. When you declare a function and you are writing out the actual code for the function, you need to give the passed values a data type. So it needs to be:
int calsum(int x, int y, int z)
Also, you need to declare "int d" outside the function, otherwise it will move out of scope once you leave the function and the program won't have a variable d (because you only declare the "int d" in the function, not in the main).
Reply:It should not be void calsum(x,y,z). You are not returning any value. In the main you are expecting a value. So, its giving error.
Make it, int calsum(x,y,z).
--
Nithin.
http://www.bluminut.com
Reply:You'll also get an error on the line with printf("\nSum=%d,sum"); It should be printf("\nSum=%d",sum); Your 2nd quotation is in the wrong place.
In the future, let us know what the error says and we can give better feedback.
Reply:void calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
return (d)
}
i think this line is the one giving errors because x, y, z are parameters and used without being declared, try this:
void calsum(int x,int y, int z)
{
int d;
d=x+y+z;
return (d)
}
Subscribe to:
Posts (Atom)