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
Wednesday, July 8, 2009
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)
}
C++ fourier code?
I would like to know where I can find a complete c++ code about fourier analisys. I need to know how to find all the sinusoids starting from a waveforn. I've tried with the FFT and DFT code but I have always magnitude and phase wrongs. Now I want to see how work a correct code.
C++ fourier code?
http://www.fftw.org/
Reply:check out http://www.pscode.com for great examples.
C++ fourier code?
http://www.fftw.org/
Reply:check out http://www.pscode.com for great examples.
Translate some code from C++ to Java?
i have the following code. i believe is in C++ and i want to translate it in to Java
i already done some myself, but i am new to Java and don't really understand C++
Thanks in advance
The program take in some numbers and find the first and last of the number 12. The program print 0 if the number 12 is not there .For example if the 4th data is the only 12, then the value 4 should be printed for the first and last occurrence.
C++ code
int main(){
int first, last, input;
first = last = 0;
cout %26lt;%26lt; "Enter a list of values, ^d to exit: ";
for (int i=0; cin %26gt;%26gt; input; i++){
if (input == 12 ){
if (first == 0){
first = last = i+1;
}
else last = i+1;
}
}
cout %26lt;%26lt; "First: " %26lt;%26lt; first %26lt;%26lt; endl;
cout %26lt;%26lt; "Last: " %26lt;%26lt; last %26lt;%26lt; endl;
return(0);
}
Java Code
int line;
int frist;
int last;
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) ); line =Integer.parseInt(br.readLine()) ;
for ( int i=0; i%26lt;number.length; i++ )
if (line==12) {
if(first==0){
Translate some code from C++ to Java?
Here's another version:
int f=0, l=0,i=0,val = 0;
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
while (true) {
try {
val = Integer.parseInt(br.readLine());
++i;
if (val == 12) {
f = (f == 0)? i:f;
l = i;
}
} catch (NumberFormatException n) {
break;
}
catch (IOException io) {
break;
}
}
System.out.println("first = " + f + " and last = " + l);
Reply:Buffered reader has to have a try / catch clause. The newer Scanner is much better.
================
import java.util.Scanner;
public class CompareCpluplus {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
boolean run = true;
while (run) {
System.out.print("Enter a list of numbers, or n to exit: ");
String s = scanner.nextLine();
// check to see if they used commas
String delimiter = (s.indexOf(",") %26gt; -1 ? "," : " ");
String[] ss = s.split(delimiter);
int first = 0;
int last = 0;
for (int i = 0; i %26lt; ss.length; i++) {
if ( Integer.parseInt( ss[i] ) == 12 ||
Integer.parseInt( ss[i] ) == 0 ) {
// ?? weird, always true, first loop
first = last = i + 1;
}
else
last = i + 1;
}
System.out.println("first is: " + first);
System.out.println("last is: " + last);
System.out.println(
"==============");
System.out.print("Do another? ");
run = (!scanner.nextLine().
equalsIgnoreCase("n")) ?
true : false;
}
}
}
============
and, I had to break some lines of code, java doesn't care about white space, but Yahoo! Answers is funny about longWordsWithoutSpaces
dogwood
i already done some myself, but i am new to Java and don't really understand C++
Thanks in advance
The program take in some numbers and find the first and last of the number 12. The program print 0 if the number 12 is not there .For example if the 4th data is the only 12, then the value 4 should be printed for the first and last occurrence.
C++ code
int main(){
int first, last, input;
first = last = 0;
cout %26lt;%26lt; "Enter a list of values, ^d to exit: ";
for (int i=0; cin %26gt;%26gt; input; i++){
if (input == 12 ){
if (first == 0){
first = last = i+1;
}
else last = i+1;
}
}
cout %26lt;%26lt; "First: " %26lt;%26lt; first %26lt;%26lt; endl;
cout %26lt;%26lt; "Last: " %26lt;%26lt; last %26lt;%26lt; endl;
return(0);
}
Java Code
int line;
int frist;
int last;
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) ); line =Integer.parseInt(br.readLine()) ;
for ( int i=0; i%26lt;number.length; i++ )
if (line==12) {
if(first==0){
Translate some code from C++ to Java?
Here's another version:
int f=0, l=0,i=0,val = 0;
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
while (true) {
try {
val = Integer.parseInt(br.readLine());
++i;
if (val == 12) {
f = (f == 0)? i:f;
l = i;
}
} catch (NumberFormatException n) {
break;
}
catch (IOException io) {
break;
}
}
System.out.println("first = " + f + " and last = " + l);
Reply:Buffered reader has to have a try / catch clause. The newer Scanner is much better.
================
import java.util.Scanner;
public class CompareCpluplus {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
boolean run = true;
while (run) {
System.out.print("Enter a list of numbers, or n to exit: ");
String s = scanner.nextLine();
// check to see if they used commas
String delimiter = (s.indexOf(",") %26gt; -1 ? "," : " ");
String[] ss = s.split(delimiter);
int first = 0;
int last = 0;
for (int i = 0; i %26lt; ss.length; i++) {
if ( Integer.parseInt( ss[i] ) == 12 ||
Integer.parseInt( ss[i] ) == 0 ) {
// ?? weird, always true, first loop
first = last = i + 1;
}
else
last = i + 1;
}
System.out.println("first is: " + first);
System.out.println("last is: " + last);
System.out.println(
"==============");
System.out.print("Do another? ");
run = (!scanner.nextLine().
equalsIgnoreCase("n")) ?
true : false;
}
}
}
============
and, I had to break some lines of code, java doesn't care about white space, but Yahoo! Answers is funny about longWordsWithoutSpaces
dogwood
Saturday, May 9, 2009
Code c# ? help with c# code Notepad ?
Can you share for me code of Notepad by C# ? Thank you
Code c# ? help with c# code Notepad ?
I presume you want to create a "notepad" application using C#. If you do a good sample is available at http://www.freevbcode.com/ShowCode.asp?I... .
Code c# ? help with c# code Notepad ?
I presume you want to create a "notepad" application using C#. If you do a good sample is available at http://www.freevbcode.com/ShowCode.asp?I... .
Help with C# code?
im having trouble with this code can anyone tell me what I'm doing wrong?
using System;
namespace JenkinsNutShop
{
class NutSales
{
static void Main()
{
// this is where I am listing the name of the business:
Console.WriteLine("JENKINS NUT SHOP");
string customerName;
double pounds;
double subTotal = 0;
string nutName = "";
char nutType;
double totalPrice;
double tax;
GetUserInput(out customerName, out nutType, out pounds);
CalcNutCosts(pounds, nutType, ref subTotal, ref nutName, out totalPrice, out tax);
DisplayReceipt(pounds,nutName,subTotal, tax, totalPrice);
}
public static void GetUserInput(out string customerName, out char nutType, out double pounds)
{
string userin;
// Prompting user for type of nuts they want to purchase:
Console.WriteLine(); //this is making a blank line
// Prompting user for the number of pounds they are ordering :
// Prompting user to input pounds of puchase
Console.Write("Customer name: ");
customerName = Console.ReadLine();
Console.WriteLine("What type of nuts would you like?");
Console.WriteLine("Please type an upper case letter or lower case letter to represent the nuts you wish to buy");
Console.WriteLine("Type in the letter \"C\" for Cashews");
Console.WriteLine("Type in the letter \"A\" for Almonds");
Console.WriteLine("Type in the letter \"P\" for Peanuts");
Console.WriteLine("Type in the letter \"W\" for Walnuts");
Console.WriteLine(); // enters a black line
Console.Write("Nut Type: ");
userin = Console.ReadLine();
nutType = Convert.ToChar(userin);
Console.Write("How many pounds would you like? ");
userin = Console.ReadLine();
pounds = Convert.ToDouble(userin);
}
public static void CalcNutCosts(double pounds, char nutType, ref double subTotal, ref string nutName, out double totalPrice, out double tax)
{
const double CASHEWS = 6.50;
const double ALMONDS = 7.25;
const double PEANUTS = 2.39;
const double WALNUTS = 2.79;
switch(nutType)
{
case'C':
case'c':
subTotal = pounds * CASHEWS;
nutName = "Cashew";
break;
case'A':
case'a':
subTotal = pounds * ALMONDS;
nutName = "Almonds";
break;
case'P':
case'p':
subTotal = pounds * PEANUTS;
nutName = "Peanuts";
break;
case'W':
case'w':
subTotal = pounds * WALNUTS;
nutName = "Walnuts";
break;
tax = subTotal * .0675;
totalPrice = subTotal + tax;
}
}
public static void DisplayReceipt(double pounds,string nutName,double subTotal, double tax, double totalPrice)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Your receipt:");
Console.WriteLine();
Console.WriteLine("{0} pounds of {1} nuts. Subtotal \t {2:C}", pounds, nutName, subTotal);
Console.WriteLine(" Tax \t {0:C}",tax);
Console.WriteLine(" Total \t {0:C}",totalPrice);
Console.WriteLine();
Console.WriteLine("Thank you for shopping at Jenkins Nut Shop \n \n");
}
}
}
Help with C# code?
As previously noted, no one wants to wade through code like this.
Please provide any specific error messages or things that are happening that you don't want to have happen.
And please, post your code on a Web server:
http://www.dougv.com/blog/2006/12/13/a-r...
Reply:One problem i notice is that in your CalcNutCosts function these statements are unreachable:
tax = subTotal * .0675;
totalPrice = subTotal + tax;
because you have your break; statement above them but you have the closing } for the switch statement after them and since you used the parameter modifier out on tax and totalPrice you will get an error because you are unable to set their values. Try moving the closing } of your switch statement after the break; but before where you set the value of tax and totalPrice like this:
case'W':
case'w':
subTotal = pounds * WALNUTS;
nutName = "Walnuts";
break;
}
tax = subTotal * .0675;
totalPrice = subTotal + tax;
}
Reply:That's a lot of code to eye over. Any specifics on the issue?
using System;
namespace JenkinsNutShop
{
class NutSales
{
static void Main()
{
// this is where I am listing the name of the business:
Console.WriteLine("JENKINS NUT SHOP");
string customerName;
double pounds;
double subTotal = 0;
string nutName = "";
char nutType;
double totalPrice;
double tax;
GetUserInput(out customerName, out nutType, out pounds);
CalcNutCosts(pounds, nutType, ref subTotal, ref nutName, out totalPrice, out tax);
DisplayReceipt(pounds,nutName,subTotal, tax, totalPrice);
}
public static void GetUserInput(out string customerName, out char nutType, out double pounds)
{
string userin;
// Prompting user for type of nuts they want to purchase:
Console.WriteLine(); //this is making a blank line
// Prompting user for the number of pounds they are ordering :
// Prompting user to input pounds of puchase
Console.Write("Customer name: ");
customerName = Console.ReadLine();
Console.WriteLine("What type of nuts would you like?");
Console.WriteLine("Please type an upper case letter or lower case letter to represent the nuts you wish to buy");
Console.WriteLine("Type in the letter \"C\" for Cashews");
Console.WriteLine("Type in the letter \"A\" for Almonds");
Console.WriteLine("Type in the letter \"P\" for Peanuts");
Console.WriteLine("Type in the letter \"W\" for Walnuts");
Console.WriteLine(); // enters a black line
Console.Write("Nut Type: ");
userin = Console.ReadLine();
nutType = Convert.ToChar(userin);
Console.Write("How many pounds would you like? ");
userin = Console.ReadLine();
pounds = Convert.ToDouble(userin);
}
public static void CalcNutCosts(double pounds, char nutType, ref double subTotal, ref string nutName, out double totalPrice, out double tax)
{
const double CASHEWS = 6.50;
const double ALMONDS = 7.25;
const double PEANUTS = 2.39;
const double WALNUTS = 2.79;
switch(nutType)
{
case'C':
case'c':
subTotal = pounds * CASHEWS;
nutName = "Cashew";
break;
case'A':
case'a':
subTotal = pounds * ALMONDS;
nutName = "Almonds";
break;
case'P':
case'p':
subTotal = pounds * PEANUTS;
nutName = "Peanuts";
break;
case'W':
case'w':
subTotal = pounds * WALNUTS;
nutName = "Walnuts";
break;
tax = subTotal * .0675;
totalPrice = subTotal + tax;
}
}
public static void DisplayReceipt(double pounds,string nutName,double subTotal, double tax, double totalPrice)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Your receipt:");
Console.WriteLine();
Console.WriteLine("{0} pounds of {1} nuts. Subtotal \t {2:C}", pounds, nutName, subTotal);
Console.WriteLine(" Tax \t {0:C}",tax);
Console.WriteLine(" Total \t {0:C}",totalPrice);
Console.WriteLine();
Console.WriteLine("Thank you for shopping at Jenkins Nut Shop \n \n");
}
}
}
Help with C# code?
As previously noted, no one wants to wade through code like this.
Please provide any specific error messages or things that are happening that you don't want to have happen.
And please, post your code on a Web server:
http://www.dougv.com/blog/2006/12/13/a-r...
Reply:One problem i notice is that in your CalcNutCosts function these statements are unreachable:
tax = subTotal * .0675;
totalPrice = subTotal + tax;
because you have your break; statement above them but you have the closing } for the switch statement after them and since you used the parameter modifier out on tax and totalPrice you will get an error because you are unable to set their values. Try moving the closing } of your switch statement after the break; but before where you set the value of tax and totalPrice like this:
case'W':
case'w':
subTotal = pounds * WALNUTS;
nutName = "Walnuts";
break;
}
tax = subTotal * .0675;
totalPrice = subTotal + tax;
}
Reply:That's a lot of code to eye over. Any specifics on the issue?
Subscribe to:
Posts (Atom)