SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and
initialize them with 3, 5, 7, 9, 11, 13, 15 Call function sum with the array and size, both passed in
pointer representations Print each element of the array Print the sum In function sum: Take the
parameters in pointer representations,
Solution
// C code
#include
#include
#include
#include
// sum function using pointers to add elements of array
int sumArray(int *array, int *SIZE)
{
int i;
int sum = 0;
for (i = 0; i < *SIZE; ++i)
{
sum = sum + *(array+i);
}
return sum;
}
int main()
{
int SIZE = 7;
int array[] = {3,5,7,9,11,13,15};
// ddeclare and initialize pointer
int *ptr;
ptr = array;
// pass array pointer and size of array to function
int sum = sumArray(ptr,&SIZE);
printf("Array: ");
int i;
for (i = 0; i < SIZE; ++i)
{
printf("%d ",array[i]);
}
printf(" ");
printf("Sum of array: %d ",sum);
return 0;
}
/*
output:
Array: 3 5 7 9 11 13 15
Sum of array: 63
*/

More Related Content

Similar to Write a C code to perform the following tasks In main Define an arr.pdf

PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
mallik3000
 

Similar to Write a C code to perform the following tasks In main Define an arr.pdf (20)

PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
using C++ to Write a program to demonstrate an array of 10 elements an.docx
using C++ to Write a program to demonstrate an array of 10 elements an.docxusing C++ to Write a program to demonstrate an array of 10 elements an.docx
using C++ to Write a program to demonstrate an array of 10 elements an.docx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
C arrays
C arraysC arrays
C arrays
 
Pointer
PointerPointer
Pointer
 
python.pdf
python.pdfpython.pdf
python.pdf
 
C programming
C programmingC programming
C programming
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Python 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptx
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 

More from fabmallkochi

Discuss the requirements for developing a Security Education, Tranin.pdf
Discuss the requirements for developing a Security Education, Tranin.pdfDiscuss the requirements for developing a Security Education, Tranin.pdf
Discuss the requirements for developing a Security Education, Tranin.pdf
fabmallkochi
 
An ethic of virtue implies that moral relativism is correct, while.pdf
An ethic of virtue implies that moral relativism is correct, while.pdfAn ethic of virtue implies that moral relativism is correct, while.pdf
An ethic of virtue implies that moral relativism is correct, while.pdf
fabmallkochi
 
A survey of large Latin American firms showed that many have expande.pdf
A survey of large Latin American firms showed that many have expande.pdfA survey of large Latin American firms showed that many have expande.pdf
A survey of large Latin American firms showed that many have expande.pdf
fabmallkochi
 
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdfApril 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
fabmallkochi
 
6. Compare the Populist and Progressive movements. Critically analy.pdf
6. Compare the Populist and Progressive movements. Critically analy.pdf6. Compare the Populist and Progressive movements. Critically analy.pdf
6. Compare the Populist and Progressive movements. Critically analy.pdf
fabmallkochi
 
Why do we believe galaxy clusters are rich in dark matter What is b.pdf
Why do we believe galaxy clusters are rich in dark matter  What is b.pdfWhy do we believe galaxy clusters are rich in dark matter  What is b.pdf
Why do we believe galaxy clusters are rich in dark matter What is b.pdf
fabmallkochi
 
Using Theodore Levitt’s central thesis of The Globalization of mar.pdf
Using Theodore Levitt’s central thesis of The Globalization of mar.pdfUsing Theodore Levitt’s central thesis of The Globalization of mar.pdf
Using Theodore Levitt’s central thesis of The Globalization of mar.pdf
fabmallkochi
 
The researchers found RNA, but that was not enough to conclude that t.pdf
The researchers found RNA, but that was not enough to conclude that t.pdfThe researchers found RNA, but that was not enough to conclude that t.pdf
The researchers found RNA, but that was not enough to conclude that t.pdf
fabmallkochi
 
could you please answer the all questions in C langauge In your own .pdf
could you please answer the all questions in C langauge In your own .pdfcould you please answer the all questions in C langauge In your own .pdf
could you please answer the all questions in C langauge In your own .pdf
fabmallkochi
 

More from fabmallkochi (20)

Establish the identity sin (2theta)[costheta+cos(3theta)]=-cos(2the.pdf
Establish the identity sin (2theta)[costheta+cos(3theta)]=-cos(2the.pdfEstablish the identity sin (2theta)[costheta+cos(3theta)]=-cos(2the.pdf
Establish the identity sin (2theta)[costheta+cos(3theta)]=-cos(2the.pdf
 
Discuss the requirements for developing a Security Education, Tranin.pdf
Discuss the requirements for developing a Security Education, Tranin.pdfDiscuss the requirements for developing a Security Education, Tranin.pdf
Discuss the requirements for developing a Security Education, Tranin.pdf
 
Determine whether or not the vectors are linearly dependent. Determi.pdf
Determine whether or not the vectors are linearly dependent. Determi.pdfDetermine whether or not the vectors are linearly dependent. Determi.pdf
Determine whether or not the vectors are linearly dependent. Determi.pdf
 
DBA1. Provide five reasons for creating a conceptual data model..pdf
DBA1. Provide five reasons for creating a conceptual data model..pdfDBA1. Provide five reasons for creating a conceptual data model..pdf
DBA1. Provide five reasons for creating a conceptual data model..pdf
 
By using MIMO technique, the throughput can be increased greatly wit.pdf
By using MIMO technique, the throughput can be increased greatly wit.pdfBy using MIMO technique, the throughput can be increased greatly wit.pdf
By using MIMO technique, the throughput can be increased greatly wit.pdf
 
Between 1840 and 1850, the United States gained one-third more land .pdf
Between 1840 and 1850, the United States gained one-third more land .pdfBetween 1840 and 1850, the United States gained one-third more land .pdf
Between 1840 and 1850, the United States gained one-third more land .pdf
 
An ethic of virtue implies that moral relativism is correct, while.pdf
An ethic of virtue implies that moral relativism is correct, while.pdfAn ethic of virtue implies that moral relativism is correct, while.pdf
An ethic of virtue implies that moral relativism is correct, while.pdf
 
A survey of large Latin American firms showed that many have expande.pdf
A survey of large Latin American firms showed that many have expande.pdfA survey of large Latin American firms showed that many have expande.pdf
A survey of large Latin American firms showed that many have expande.pdf
 
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdfApril 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
April 18, 2018, Lora and Andrew Buck come to your law office for an .pdf
 
4. Which pair of elements has the smaller element listed first (a) A.pdf
4. Which pair of elements has the smaller element listed first (a) A.pdf4. Which pair of elements has the smaller element listed first (a) A.pdf
4. Which pair of elements has the smaller element listed first (a) A.pdf
 
6. Compare the Populist and Progressive movements. Critically analy.pdf
6. Compare the Populist and Progressive movements. Critically analy.pdf6. Compare the Populist and Progressive movements. Critically analy.pdf
6. Compare the Populist and Progressive movements. Critically analy.pdf
 
Why do we believe galaxy clusters are rich in dark matter What is b.pdf
Why do we believe galaxy clusters are rich in dark matter  What is b.pdfWhy do we believe galaxy clusters are rich in dark matter  What is b.pdf
Why do we believe galaxy clusters are rich in dark matter What is b.pdf
 
Which of the following is trueThe definition of tool is too variabl.pdf
Which of the following is trueThe definition of tool is too variabl.pdfWhich of the following is trueThe definition of tool is too variabl.pdf
Which of the following is trueThe definition of tool is too variabl.pdf
 
Who are we as a country With all the demographic changes currently .pdf
Who are we as a country With all the demographic changes currently .pdfWho are we as a country With all the demographic changes currently .pdf
Who are we as a country With all the demographic changes currently .pdf
 
Using Theodore Levitt’s central thesis of The Globalization of mar.pdf
Using Theodore Levitt’s central thesis of The Globalization of mar.pdfUsing Theodore Levitt’s central thesis of The Globalization of mar.pdf
Using Theodore Levitt’s central thesis of The Globalization of mar.pdf
 
1. Give five reasons why a person might want to extract DNA from cel.pdf
1. Give five reasons why a person might want to extract DNA from cel.pdf1. Give five reasons why a person might want to extract DNA from cel.pdf
1. Give five reasons why a person might want to extract DNA from cel.pdf
 
Transfer taxes are handled under which tax topics Transfer ta.pdf
Transfer taxes are handled under which tax topics  Transfer ta.pdfTransfer taxes are handled under which tax topics  Transfer ta.pdf
Transfer taxes are handled under which tax topics Transfer ta.pdf
 
This project creates a custom linked list structure. It serves.pdf
This project creates a custom linked list structure.      It serves.pdfThis project creates a custom linked list structure.      It serves.pdf
This project creates a custom linked list structure. It serves.pdf
 
The researchers found RNA, but that was not enough to conclude that t.pdf
The researchers found RNA, but that was not enough to conclude that t.pdfThe researchers found RNA, but that was not enough to conclude that t.pdf
The researchers found RNA, but that was not enough to conclude that t.pdf
 
could you please answer the all questions in C langauge In your own .pdf
could you please answer the all questions in C langauge In your own .pdfcould you please answer the all questions in C langauge In your own .pdf
could you please answer the all questions in C langauge In your own .pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Write a C code to perform the following tasks In main Define an arr.pdf

  • 1. Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3, 5, 7, 9, 11, 13, 15 Call function sum with the array and size, both passed in pointer representations Print each element of the array Print the sum In function sum: Take the parameters in pointer representations, Solution // C code #include #include #include #include // sum function using pointers to add elements of array int sumArray(int *array, int *SIZE) { int i; int sum = 0; for (i = 0; i < *SIZE; ++i) { sum = sum + *(array+i); } return sum; } int main() { int SIZE = 7; int array[] = {3,5,7,9,11,13,15}; // ddeclare and initialize pointer int *ptr; ptr = array; // pass array pointer and size of array to function int sum = sumArray(ptr,&SIZE); printf("Array: "); int i; for (i = 0; i < SIZE; ++i) {
  • 2. printf("%d ",array[i]); } printf(" "); printf("Sum of array: %d ",sum); return 0; } /* output: Array: 3 5 7 9 11 13 15 Sum of array: 63 */