Thanks' sepp2k. I appreciate. That multiplication cause me lot of pain. Thanks'

``````max <- 0

for(i in 1:1000){
x1 <- runif(1:10, 1)
x2 <- runif(1:10, 1)
x3 <- runif(1:10, 1)
z <- (exp(x1)+x2)^2 + (3(1-x3)^2)
if(z > max)
max <- z
}
print(max)``````

This has to print maximum number between 1 to 10000 and 'z' is the formula.
I am getting error "Error: attempt to apply non-function".

Thanks'.

Sorry. The code is ok.
The program to compute pi(n), which is the number of prime numbers < n. It will print out pi(n) for values of n that are powers of 10 from 10 to 10^8.
For example, pi(2) = 1 and pi(10) = 4.1

Output will be in four columns.
1) The first will contain n.
2) The second will contain pi(n)
3) The third will contain n/ln(n)
4) The fourth will contain the ration of pi(n) to n/ln(n).
An extremely important theorem of number theory says that as n goes to pi, the ratio of pi(n) to n/ln(n) converges to 1.

So what should I do to output as above?
Thanks'

I need to output in four columns.
1) The first will contain n.
2) The second will contain pi(n)
3) The third will contain n/ln(n)
4) The fourth will contain the ration of pi(n) to n/ln(n).

``````#include <stdio.h>
#include <string.h>
#include <stdint.h>

#define MAXN  100000000  /* maximum value of N */
#define P1    1562501    /* = ceil(MAXN/64) */
#define P2    50000000   /* = ceil(MAXN/2) */
#define P3    5000       /* = ceil(ceil(sqrt(MAXN))/2) */

uint32_t sieve[P1];

#define GET(b) ((sieve[(b)>>5]>>((b)&31))&1)

void make()
{
uint32_t i, j, k;
memset(sieve, 0, sizeof(sieve));
for (k = 1; k <= P3; k++)
if (GET(k)==0) for(j=2*k+1,i=2*k*(k+1);i<P2;i+=j) sieve[i>>5]|=1<<(i&31);
}

int isprime(int p) { return p==2 || (p>2 && (p&1)==1 && (GET((p-1)>>1)==0)); }

int main()
{
int i, n;
make();
for (n = 0, i = 0; i <= MAXN; i++)
if (isprime(i)) n++;
printf("The number of primes below 10^8 is %d.\n", n);
return 0;
}``````

Yes. That's the problem. Thanks' stultuske.

Write a program with subclass InterestAccount and CDAccount. The driver program (Account.java) must perform following transactions:

1) Print the name of your bank and yours at the top.
2) Creat an Intrest Account for "Benjamin Franklin" with PIN of 1234 and a starting balance of \$100.
3) Creat a CD Account for "William Shakespeare" with PIN of 4321 and a statring balance of &1000.
4) Do three monthly updates, printing out the amount in each account.
5) Deposit \$1000 in the Amazon's accounts.
6) Withdraw \$500 from Ebay's accounts.
7) Do at least 10 times or more monthly updates, printing out the amounts in each account.

Program should be stores in Account.java, Acount types IntrestAccount.java and CDAccount.java.

Account.java

``````    //Demonstrates the BankAccount and derived classes

import java.text.*;         // to use Decimal Format
import java.util.*;
import java.io.*;
import AccountKinds.*;

public class Account extends Object
{
public static void main(String[] args)
{
double put_in = 500;
double take_out = 1000;

DecimalFormat myFormat;
String money;
String money_in;
String money_out;
boolean completed;

// to get 2 decimals every time
myFormat = new DecimalFormat("#.00");

//to test the Checking Account class
CheckingAccount anAccount;
anAccount = new CheckingAccount ("Benjamin Franklin", 1234);
System.out.println ("Account Number "
+ anAccount.getAccountNumber() + "-10"+
" belonging to " + anAccount.getOwner());
money  = myFormat.format(anAccount.getBalance());
System.out.println ("Initial balance = \$" + money);
anAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money  = myFormat.format(anAccount.getBalance());
System.out.println ("After deposit of \$" + money_in
+ ",  balance = \$" + money);
completed = anAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money  = myFormat.format(anAccount.getBalance());
if (completed) ...``````

Write a java code, Extend class Fraction by adding methods for the arithmetic operators +, - and /, and the six relational operators (==, !=, <, >, <= and >=). Then compute a menu-driven fraction calculator. Program shoud do all 9 arithmetic operations and 0 to stop. User should first asked for the options 1 to 0 and then asked for first fraction and second fraction. It will keep running until user enter 0 to stop.

Fraction class code:

``````import java.util.*;  // Scanner
import java.io.*;    // PrintStream

public class Fraction extends Object
{
private int myNumerator, myDenominator;
public Fraction()
{
setFraction(0,1);
}

public Fraction (int numerator,int  denominator)
{
setFraction(numerator, denominator);
}

public void setFraction(int numerator, int denominator)

{
if (denominator == 0)
throw new IllegalArgumentException("Denominator cannot be 0!");
myNumerator = numerator;
myDenominator = denominator;
simplify();
}

public int getNumerator()
{
return myNumerator;
}

public int getDenominator()
{
return myDenominator;
}

{
String fract = sc.next();   //"3/4"
//  System.out.println();
StringTokenizer parser = new StringTokenizer(fract, "/");
if (parser.countTokens() !=2)
throw new IllegalArgumentException("Bad format for Fraction!");
String first = parser.nextToken();
int num = Integer.parseInt(first);
//myNumerator = Integer.parseInt(first);
String second = parser.nextToken();
//myDenominator = Integer.parseInt(second);
int denom = Integer.parseInt(second);
setFraction(num,denom);

}

public Fraction times(Fraction rightOperand)
{
int num = myNumerator * rightOperand.getNumerator();
int denom = myDenominator * rightOperand.getDenominator();
return new Fraction(num, denom);

}

public Fraction divide(Fraction divi)
{
int num = myNumerator * divi.myDenominator;
int denom = myDenominator * divi.myNumerator;
return new Fraction(num, denom);
}

{ ...``````

How to check for the file name, If there are no command line arguments, it will read from standard input. If "-" is given as a file name, it will read from standard input. As with the Linux version of head, program should print the file name (or standard input) if there are two or more command line arguments. Use the same format as the Linux version of head.

C program that extends my.c to take file names as command line arguments. It will read each named file and write the first 10 lines to standard output. If there are no command line arguments, it will read from standard input. If "-" is given as a file name, it will read from standard input.

As with the Linux version of head, program should print the file name (or standard input) if there are two or more command line arguments. Use the same format as the Linux version of head.

Write a function *void do_file(FILE infile)** that takes a stream and processes that stream, open the files in main and call do_file to process them.

Check to make sure that files open properly and print appropriate error messages to stderr (in the usual Unix style - using errno) if not. If a file cannot be opened, go ahead and process the other files.

My my.c Code is here:

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void do_file(FILE * infile){

int lineCount = 0; // keeps track of lines
char line ;

// read file line by line
while(fgets(line, 128, infile) != NULL){
lineCount++;
if(lineCount <= 10)
fputs(line, stdout); // write line to stdout
}
}

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

do_file(stdin);
return 0;
}``````

Thanks.

Thanks' deceptikon.

Why am I getting "Segmentation fault: 11". I have to compile this code by redirecting file from stdin like ./my < data.

Thanks. It works.

Thanks deceptikon. But it still reads the hole file. Here is my updates code:

``````#include <stdio.h>

int main()
{
char *inname;
FILE *infile;
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
char fileName;

scanf("%s",fileName);
infile=fopen(fileName,"r");

if (!infile)
{
printf("Couldn't open file %s for reading.\n", inname);
return 0;
}

for (int i = 0; i < 10 && fgets(line_buffer, sizeof(line_buffer), infile); i++)
{
printf("%s\n", line_buffer);
}
return 0;
}``````

I made few changes, but still I can not able to read 10 lines. My program reads all the line untill end. So how do I read only 10 line of input file. Here is my code:

``````#include <stdio.h>

int main()
{
char *inname;
FILE *infile;
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
char fileName;

scanf("%s",fileName);
infile=fopen(fileName,"r");

if (!infile)
{
printf("Couldn't open file %s for reading.\n", inname);
return 0;
}

while (fgets(line_buffer, sizeof(line_buffer), infile))
{
printf("%s\n", line_buffer);
}
return 0;
}``````

How to convert C++ code to C.

Write a program in C that asks a user for the name of a file. The program should display the first 10 lines of the file on the screen (the "head" of the file). If the file has fewer than 10 lines, the entire file should be displayed, with a message indicating the entire file has been displayed.

``````    \//Run the program using visual studio 2010 vc++
/**
*This program ask the user to enter the name of the text file
*and displays the ten lines of the file and if the file having less than 10 line than
*display the output that the total file has been displayed
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//start of main funtion
int main()
{
//variable declaration
string filename;
char ch;
//char line;
//create file stream object to the class fstream
fstream fin;
cout<<"Enter a filename"<
cin>>filename;
//open that file in read mode
fin.open(filename,ios::in);
//check the file is opened successfully
if(!fin)
{
cout<<"Error while opening file "<
}
else
{
int counter=0;
//checking for end of the file

while((ch=fin.get())!=EOF && counter<=10 )
{
//getline funtion to take one line at a time
if(ch=='\n' )
{
counter++;
}
//display to the screen
cout<

}
//display the message if number of lines are less than ten
if(counter<10)
{
cout<<<"------------------------------"<
cout<<"ENTIRE FILE HAS BEEN DISPLAYED"<
}

}
//to pause the output
system("pause");
return 0;
}
//end of main function``````

Thanks' Ancient Dragon. I appreciate.

Thanks. So what should I do to redirecting the contents of numbers into rev program?

I have problem reading file form command line.
I am trying to do " abcd: ./rev < numbers", but it gives me an error:

``````./rev < numbers
Usage :
./rev FILENAME``````

Here is the code:

``````#include <stdio.h>
#include <stdlib.h>

void reverse(FILE * file)
{
int fscanf_return_value;
char x;
fscanf_return_value = fscanf(file,"%c",&x) ;
if(fscanf_return_value == EOF)
{ //fscanf returns EOF as the END OF FILE
return;
}
reverse(file); //Get the next char
//do something with the char e.g. print
putchar(x);
return;
}

int main(int argc, char *argv[])
{
int i;
FILE *fd;
if (argc!=2)
{
printf("Usage : \n %s FILENAME\n",argv);
exit(0);
}
if(!(fd=fopen(argv,"r")))
{
printf("Opening file error\n");
exit(1);
}
reverse(fd);
printf("\n\n\t---\tenoD - Done\t---\n");
exit(0);
}``````

Also I can't read a file one per line, and prints them back out in reverse order. I need output like:
.
.
.
12
11
10
9
8
7
6
5
4
3
2
1
0
But I am getting:
21
11
01
9
8
7
6
5
4
3
2
1
0

I am blank at this point. I tried but, but I have no time.
Thanks' all of you for your help. I appreciate.

OK. I already included math library. I am using std=gnu99.

I am getting wrong output. My Code is following, in which I have to use function unsigned long powerof2(int n);

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//int powerof2(int n);
unsigned long powerof2(int n);
int main(void)
{
printf(" n 2^n\n");
printf("-- ---\n");
for (int n=0; n<65 ;n++)
{
//printf(" n 2^n\n");
printf("%d %ld\n", n, powerof2(n));
}
}

unsigned long powerof2(int n)
{
unsigned long y;
for(int j=0;j<65;j++)
{
if (n == 64)
y = 0;
else
y = 1 << n;
}
return y;
}

Output:

n 2^n
-- ---
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
21 2097152
22 4194304
23 8388608
24 16777216
25 33554432
26 67108864
27 134217728
28 268435456
29 536870912
30 1073741824
31 -2147483648
32 1
33 2
34 4
35 8
36 16
37 32
38 64
39 128
40 256
41 512
42 1024
43 2048
44 4096
45 8192
46 16384
47 32768
48 65536
49 131072
50 262144
51 524288
52 1048576
53 2097152
54 4194304
55 8388608
56 16777216
57 33554432
58 67108864
59 134217728
60 268435456
61 536870912
62 1073741824
63 -2147483648
64 0``````

I need:

``````n 2^n
-- ---
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 ...``````

I got it nullptr. Thanks'.

Ok I solved the errors. But now how to do it with left shift command instead of multiplication?

I am not sure what you mean?

I am trying to print:

0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
21 2097152
22 4194304
23 8388608
24 16777216
25 33554432
26 67108864
27 134217728
28 268435456
29 536870912
30 1073741824
31 -2147483648
32 0

Thsi is my code:

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int powerof2(int n);
int main(void)
{
for (int n=0; n<32 ;n++)
{
printf("n 2^n\n");
printf("%d %d\n", n, powerof2);
}
}

int powerof2(int n)
{
int power=1;
for(int j=0;j<n;++j)
{
if (j == 0)
power = 1;
else
power *= 2;
}
return power;
}``````

I have to use function - int powerof2(int n).
But it gives me an error:
power1.c: In function ‘main’:
power1.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int ()(int)’
power1.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (
)(int)’

Thanks'. Stultuske, I finally submitted with no error and I don't know what celestia trying to say.

I am geting errors:

``````Histo.java:26: incompatible types
found   : double[]
required: double
double sorted = scoreArr;
^
Histo.java:38: printHist(double[]) in Histo cannot be applied to (double)
printHist(sorted);
^
Histo.java:111: cannot find symbol
symbol  : method hasNextInt()
location: class double[]
while (sorted.hasNextInt())
^
Histo.java:113: cannot find symbol
symbol  : method nextInt()
location: class double[]
int score = sorted.nextInt();
^
4 errors``````

I did every thing I know.

I have trouble in Function, public static void printHist(double[] sorted). I can't able to display Histogram. And in, public class DoubleArrayOps, I have trouble swaping min and max.

I have to write a java program.
A certain on-line testing program records student exam results in a text file, each line of which has the form:

name examScore
Write a program that (using an array) analyzes student performance on an exam using the information from such a file. The program should input the name of the text file, and then display the worst score, the best score, the average score, the standard deviation, and a histogram -- a bar graph indicating the frequency with which a given score occurred. For example, if three people scored 74, five people scored 75, six people scored 76, no one scored 77 and two people scored 78, then that portion of the histogram should appear as:

74:
75:
76:
77:
78:

Entries below the worst or above the best should not be displayed.